Equivalent of *Nix 'which' command in PowerShell?

后端 未结 14 2196
刺人心
刺人心 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:57

    Try the where command on Windows 2003 or later (or Windows 2000/XP if you've installed a Resource Kit).

    BTW, this received more answers in other questions:

    Is there an equivalent of 'which' on Windows?

    PowerShell equivalent to Unix which command?

    0 讨论(0)
  • 2020-11-28 00:59

    My proposition for the Which function:

    function which($cmd) { get-command $cmd | % { $_.Path } }
    
    PS C:\> which devcon
    
    C:\local\code\bin\devcon.exe
    
    0 讨论(0)
  • 2020-11-28 01:00

    Use:

    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"
    }
    

    Or this version, calling the original where command.

    This version also works better, because it is not limited to bat files:

    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'
    }
    
    0 讨论(0)
  • 2020-11-28 01:03

    Here is an actual *nix equivalent, i.e. it gives *nix-style output.

    Get-Command <your command> | Select-Object -ExpandProperty Definition
    

    Just replace with whatever you're looking for.

    PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
    C:\Windows\system32\notepad.exe
    

    When you add it to your profile, you will want to use a function rather than an alias because you can't use aliases with pipes:

    function which($name)
    {
        Get-Command $name | Select-Object -ExpandProperty Definition
    }
    

    Now, when you reload your profile you can do this:

    PS C:\> which notepad
    C:\Windows\system32\notepad.exe
    
    0 讨论(0)
  • 2020-11-28 01:03

    I usually just type:

    gcm notepad
    

    or

    gcm note*
    

    gcm is the default alias for Get-Command.

    On my system, gcm note* outputs:

    [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
    

    You get the directory and the command that matches what you're looking for.

    0 讨论(0)
  • 2020-11-28 01:03

    You can install the which command from https://goprogram.co.uk/software/commands, along with all of the other UNIX commands.

    0 讨论(0)
提交回复
热议问题