我如何问PowerShell的东西在哪里?
例如,"which notepad",它根据当前路径返回notepad.exe运行的目录。
我如何问PowerShell的东西在哪里?
例如,"which notepad",它根据当前路径返回notepad.exe运行的目录。
当前回答
这是一个实际的*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
其他回答
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.
这似乎是你想要的(我在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
如果你有勺子,你可以安装一个直接克隆:
scoop install which
which notepad
这是一个实际的*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