我如何问PowerShell的东西在哪里?
例如,"which notepad",它根据当前路径返回notepad.exe运行的目录。
我如何问PowerShell的东西在哪里?
例如,"which notepad",它根据当前路径返回notepad.exe运行的目录。
这似乎是你想要的(我在http://huddledmasses.org/powershell-find-path/):上找到了它
Function Find-Path($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
## You could comment out the function stuff and use it as a script instead, with this line:
#param($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
if($(Test-Path $Path -Type $type)) {
return $path
} else {
[string[]]$paths = @($pwd);
$paths += "$pwd;$env:path".split(";")
$paths = Join-Path $paths $(Split-Path $Path -leaf) | ? { Test-Path $_ -Type $type }
if($paths.Length -gt 0) {
if($All) {
return $paths;
} else {
return $paths[0]
}
}
}
throw "Couldn't find a matching path of type $type"
}
Set-Alias find Find-Path
看看这个PowerShell哪个。
这里提供的代码表明:
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
我通常只输入:
gcm notepad
or
gcm note*
gcm是Get-Command的默认别名。
在我的系统上,gcm note*输出:
[27] » gcm note*
CommandType Name Definition
----------- ---- ----------
Application notepad.exe C:\WINDOWS\notepad.exe
Application notepad.exe C:\WINDOWS\system32\notepad.exe
Application Notepad2.exe C:\Utils\Notepad2.exe
Application Notepad2.ini C:\Utils\Notepad2.ini
您将获得与您正在查找的内容相匹配的目录和命令。
当我开始在PowerShell中定制我的个人资料时,我做的第一个别名是“which”。
New-Alias which get-command
要将此添加到您的配置文件中,请键入以下内容:
"`nNew-Alias which get-command" | add-content $profile
最后一行开头的' n是为了确保它将作为一个新行开始。
在Windows 2003或更高版本(或Windows 2000/XP,如果您已经安装了Resource Kit)上尝试使用where命令。
顺便说一下,这个问题在其他问题中得到了更多的答案:
Windows上有对应的“which”吗?
PowerShell相当于Unix的哪个命令?
这是一个实际的*nix等效,即它给出*nix风格的输出。
Get-Command <your command> | Select-Object -ExpandProperty Definition
用你想要的替换就行了。
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
当你把它添加到你的配置文件时,你会想要使用一个函数而不是一个别名,因为你不能对管道使用别名:
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
现在,当你重新加载你的个人资料,你可以这样做:
PS C:\> which notepad
C:\Windows\system32\notepad.exe
Use:
function Which([string] $cmd) {
$path = (($Env:Path).Split(";") | Select -uniq | Where { $_.Length } | Where { Test-Path $_ } | Get-ChildItem -filter $cmd).FullName
if ($path) { $path.ToString() }
}
# Check if Chocolatey is installed
if (Which('cinst.bat')) {
Write-Host "yes"
} else {
Write-Host "no"
}
或者这个版本,调用原来的where命令。
这个版本也更好,因为它不局限于bat文件:
function which([string] $cmd) {
$where = iex $(Join-Path $env:SystemRoot "System32\where.exe $cmd 2>&1")
$first = $($where -split '[\r\n]')
if ($first.getType().BaseType.Name -eq 'Array') {
$first = $first[0]
}
if (Test-Path $first) {
$first
}
}
# Check if Curl is installed
if (which('curl')) {
echo 'yes'
} else {
echo 'no'
}
我对Which函数的命题是:
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
与Unix的快速匹配
New-Alias which where.exe
但如果它们存在,它会返回多行,然后它就变成
function which {where.exe command | select -first 1}
我喜欢Get-Command |格式列表,或更短,使用别名的两个,只用于powershell.exe:
gcm powershell | fl
你可以找到这样的别名:
alias -definition Format-List
制表符补全使用gcm。
一次列出所有选项:
set-psreadlineoption -editmode emacs
我有这个高级功能在我的PowerShell配置文件:
function which {
<#
.SYNOPSIS
Identifies the source of a PowerShell command.
.DESCRIPTION
Identifies the source of a PowerShell command. External commands (Applications) are identified by the path to the executable
(which must be in the system PATH); cmdlets and functions are identified as such and the name of the module they are defined in
provided; aliases are expanded and the source of the alias definition is returned.
.INPUTS
No inputs; you cannot pipe data to this function.
.OUTPUTS
.PARAMETER Name
The name of the command to be identified.
.EXAMPLE
PS C:\Users\Smith\Documents> which Get-Command
Get-Command: Cmdlet in module Microsoft.PowerShell.Core
(Identifies type and source of command)
.EXAMPLE
PS C:\Users\Smith\Documents> which notepad
C:\WINDOWS\SYSTEM32\notepad.exe
(Indicates the full path of the executable)
#>
param(
[String]$name
)
$cmd = Get-Command $name
$redirect = $null
switch ($cmd.CommandType) {
"Alias" { "{0}: Alias for ({1})" -f $cmd.Name, (. { which $cmd.Definition } ) }
"Application" { $cmd.Source }
"Cmdlet" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Function" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Workflow" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"ExternalScript" { $cmd.Source }
default { $cmd }
}
}
如果你想要一个既接受管道输入又接受参数输入的命令,你应该试试这个:
function which($name) {
if ($name) { $input = $name }
Get-Command $input | Select-Object -ExpandProperty Path
}
复制粘贴命令到您的概要文件(记事本$profile)。
例子:
❯ echo clang.exe | which
C:\Program Files\LLVM\bin\clang.exe
❯ which clang.exe
C:\Program Files\LLVM\bin\clang.exe
There also always the option of using which. there are actually three ways to access which from Windows powershell, the first (not necessarily the best) wsl -e which command (this requires installation of windows subsystem for Linux and a running distro). B. gnuwin32 which is a port of several gnu binaries in .exe format as standle alone bundled lanunchers option three, install msys2 (cross compiler platform) if you go where it installed in /usr/bin you'll find many many gnu utils that are more up-to-date. most of them work as stand alone exe and can be copied from the bin folder to your home drive somewhere amd added to your PATH.
还有使用which的选项。实际上有三种方法可以从Windows powershell中访问
第一个(虽然不是最好的)是wsl(linux的windows子系统)
wsl -e which command
这需要安装Linux的windows子系统和一个正在运行的发行版。
下一个是gnuwin32,它是几个。exe格式的gnu二进制文件的移植,作为独立的捆绑启动程序 第三,安装msys2(跨编译器平台),如果你去/usr/bin中安装它,你会发现很多很多最新的gnu utils。他们中的大多数工作作为独立的exe,可以从bin文件夹复制到你的家庭驱动器的某个地方,并添加到你的路径。