我如何问PowerShell的东西在哪里?

例如,"which notepad",它根据当前路径返回notepad.exe运行的目录。


当前回答

当我开始在PowerShell中定制我的个人资料时,我做的第一个别名是“which”。

New-Alias which get-command

要将此添加到您的配置文件中,请键入以下内容:

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

最后一行开头的' n是为了确保它将作为一个新行开始。

其他回答

如果你想要一个既接受管道输入又接受参数输入的命令,你应该试试这个:

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

这是一个实际的*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

我喜欢Get-Command |格式列表,或更短,使用别名的两个,只用于powershell.exe:

gcm powershell | fl

你可以找到这样的别名:

alias -definition Format-List

制表符补全使用gcm。

一次列出所有选项:

set-psreadlineoption -editmode emacs

当我开始在PowerShell中定制我的个人资料时,我做的第一个别名是“which”。

New-Alias which get-command

要将此添加到您的配置文件中,请键入以下内容:

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

最后一行开头的' n是为了确保它将作为一个新行开始。

我对Which函数的命题是:

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

PS C:\> which devcon

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