495

我如何询问 PowerShell 某物在哪里?

例如,“哪个记事本”,它会根据当前路径返回运行 notepad.exe 的目录。

4

15 回答 15

487

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

New-Alias which get-command

要将其添加到您的个人资料中,请键入:

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

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

于 2008-09-15T17:56:32.890 回答
197

这是一个实际的*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
于 2013-06-05T20:22:09.063 回答
110

我通常只输入:

gcm notepad

或者

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

您将获得与您要查找的内容相匹配的目录和命令。

于 2008-09-15T15:22:32.760 回答
43

试试这个例子:

(Get-Command notepad.exe).Path
于 2014-04-01T04:17:28.330 回答
9

我对Which函数的提议:

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

PS C:\> which devcon

C:\local\code\bin\devcon.exe
于 2015-11-17T10:14:58.887 回答
7

与 Unix 的快速匹配which

New-Alias which where.exe

但是如果它们存在,它会返回多行,所以它变成

function which {where.exe command | select -first 1}
于 2017-04-08T19:06:36.310 回答
6

我喜欢Get-Command | Format-List,或者更短,为这两者使用别名,并且仅用于powershell.exe

gcm powershell | fl

你可以找到这样的别名:

alias -definition Format-List

制表符完成适用于gcm.

让选项卡一次列出所有选项:

set-psreadlineoption -editmode emacs
于 2017-04-11T19:29:22.910 回答
3

这似乎可以满足您的要求(我在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
于 2008-09-15T15:16:09.563 回答
2

检查这个PowerShell 哪个.

那里提供的代码表明了这一点:

($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
于 2008-09-15T15:16:27.173 回答
2

where在 Windows 2003 或更高版本(或 Windows 2000/XP,如果您已安装资源工具包)上尝试该命令。

顺便说一句,这在其他问题中得到了更多答案:

Windows上是否有相当于“which”的?

PowerShell 相当于 Unixwhich命令?

于 2011-12-13T05:14:57.143 回答
2

如果您想要一个既可以接受来自管道的输入也可以作为参数的命令,您应该尝试以下操作:

function which($name) {
    if ($name) { $input = $name }
    Get-Command $input | Select-Object -ExpandProperty Path
}

将命令复制粘贴到您的个人资料 ( notepad $profile)。

例子:

❯ echo clang.exe | which
C:\Program Files\LLVM\bin\clang.exe

❯ which clang.exe
C:\Program Files\LLVM\bin\clang.exe
于 2020-06-01T07:08:46.993 回答
0

采用:

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'
}
于 2013-12-22T11:06:40.330 回答
0

我的 PowerShell 配置文件中有这个which高级功能:

    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 }
        }
    }
于 2018-03-05T18:03:51.640 回答
0

您可以从https://goprogram.co.uk/software/commands安装该which命令以及所有其他 UNIX 命令。

于 2020-07-20T12:50:12.807 回答
0

如果你有独家新闻,你可以安装一个直接克隆:

scoop install which
which notepad
于 2021-11-06T22:44:50.440 回答