如何询问 PowerShell 某物在哪里?

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

有帮助吗?

解决方案

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

New-Alias which get-command

要将其添加到您的个人资料中,请输入以下内容:

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

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

其他提示

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

我通常只输入:

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

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

试试这个例子:

(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

我对 Which 函数的建议:

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

PS C:\> which devcon

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

尝试一下 where 在 Windows 2003 或更高版本(或者 Windows 2000/XP,如果您安装了资源工具包)上运行命令。

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

Windows 上有相当于“which”的吗?

PowerShell 相当于 Unix which 命令?

与 Unix 的快速匹配 which

New-Alias which where.exe

但它会返回多行(如果存在),那么它就变成了

$(where.exe command | select -first 1)

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

gcm powershell | fl

您可以这样查找别名:

alias -definition Format-List

制表符补全适用于 gcm.

我有这个 which 我的 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([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'
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top