在Powershell中相当于* Nix'哪个'命令?

时间:2020-03-05 18:53:42  来源:igfitidea点击:

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

解决方案

回答

这似乎可以满足要求(我在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

回答

我通常只输入:

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

我们将得到与我们要查找的目录和命令匹配的目录。

回答

在Powershell中开始自定义个人资料后,我创建的第一个别名是"哪个"。

New-Alias which get-command

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

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

`n是为了确保它将以新行开始。