Powershell中命令* Nix的等价物?

有谁知道如何问PowerShell的东西在哪里?
例如“哪个记事本”,它根据当前path返回运行notepad.exe的目录。

一旦我开始在PowerShell中定制我的configuration文件,我做的第一个别名就是“哪个”。

New-Alias which get-command 

要添加到您的个人资料,请input:

 "`nNew-Alias which get-command" | add-content $profile 

`n是要确保它将作为一个新的行开始。

这是一个实际的* nix等价物,即它提供了* nix风格的输出。

 Get-Command <your command> | Select-Object -ExpandProperty Definition 

只需要replace你正在寻找的任何东西。

 PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition C:\Windows\system32\notepad.exe 

将其添加到configuration文件时,您将需要使用函数而不是别名,因为您不能使用pipe道的别名:

 function which($name) { Get-Command $name | Select-Object -ExpandProperty Definition } 

现在,当你重新加载你的个人资料,你可以这样做:

 PS C:\> which notepad C:\Windows\system32\notepad.exe 

我通常只是input:

 gcm notepad 

要么

 gcm note* 

gcm是Get-Command的默认别名。

在我的系统上,gcm note * outputs:

 [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 

你得到的目录和命令匹配你正在寻找。

试试这个例子:

 (Get-Command notepad.exe).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 

在Windows 2003或更高版本(或Windows 2000 / XP,如果您已安装资源工具包)上尝试使用where命令: http : //ss64.com/nt/where.html

顺便说一句,这在其他线程收到更多的答案:

是否有相当于Windows的“哪个”?

Powershell相当于unix的which命令?

我对哪个函数的build议是:

function which($cmd) { get-command $cmd | % { $_.Path } }

PS C:\> which devcon

C:\local\code\bin\devcon.exe

与Unix更紧密的匹配,特别是对脚本$(which command)

 New-Alias which where.exe 
 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' } 

我喜欢get-command | 格式列表或:

 gcm powershell | fl 

你可以find这样的别名:

 alias -definition format-list 

Tab完成与gcm一起使用。