Equivalent of *Nix 'which' command in PowerShell?

后端 未结 14 2242
刺人心
刺人心 2020-11-28 00:16

How do I ask PowerShell where something is?

For instance, \"which notepad\" and it returns the directory where the notepad.exe is run from according to the current

14条回答
  •  鱼传尺愫
    2020-11-28 00:44

    This seems to do what you want (I found it on 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
    

提交回复
热议问题