Is there an equivalent of 'which' on the Windows command line?

后端 未结 26 2046
悲哀的现实
悲哀的现实 2020-11-22 00:40

As I sometimes have path problems, where one of my own cmd scripts is hidden (shadowed) by another program (earlier on the path), I would like to be able to find the full pa

26条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 00:56

    Under PowerShell, Get-Command will find executables anywhere in $Env:PATH.

    Get-Command eventvwr
    
    CommandType   Name          Definition
    -----------   ----          ----------
    Application   eventvwr.exe  c:\windows\system32\eventvwr.exe
    Application   eventvwr.msc  c:\windows\system32\eventvwr.msc
    

    It also finds PowerShell cmdlets, functions, aliases, files with custom executables extensions via $Env:PATHEXT, etc. defined for the current shell (quite akin to Bash's type -a foo) - making it a better go-to than other tools like where.exe, which.exe, etc which are unaware of these PowerShell commands.

    Finding executables using only part of the name

    gcm *disk*
    
    CommandType     Name                             Version    Source
    -----------     ----                             -------    ------
    Alias           Disable-PhysicalDiskIndication   2.0.0.0    Storage
    Alias           Enable-PhysicalDiskIndication    2.0.0.0    Storage
    Function        Add-PhysicalDisk                 2.0.0.0    Storage
    Function        Add-VirtualDiskToMaskingSet      2.0.0.0    Storage
    Function        Clear-Disk                       2.0.0.0    Storage
    Cmdlet          Get-PmemDisk                     1.0.0.0    PersistentMemory
    Cmdlet          New-PmemDisk                     1.0.0.0    PersistentMemory
    Cmdlet          Remove-PmemDisk                  1.0.0.0    PersistentMemory
    Application     diskmgmt.msc                     0.0.0.0    C:\WINDOWS\system32\diskmgmt.msc
    Application     diskpart.exe                     10.0.17... C:\WINDOWS\system32\diskpart.exe
    Application     diskperf.exe                     10.0.17... C:\WINDOWS\system32\diskperf.exe
    Application     diskraid.exe                     10.0.17... C:\WINDOWS\system32\diskraid.exe
    ...
    

    Finding custom executables

    To find other non-windows executables (python, ruby, perl, etc), file extensions for those executables need to be added to the PATHEXT environmental variable (defaults to .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL) to identify files with these extensions in the PATH as executable. As Get-Command also honours this variable, it can be extended to list custom executables. e.g.

    $Env:PATHEXT="$Env:PATHEXT;.dll;.ps1;.psm1;.py"     # temporary assignment, only for this shell's process
    
    gcm user32,kernel32,*WASM*,*http*py
    
    CommandType     Name                        Version    Source
    -----------     ----                        -------    ------
    ExternalScript  Invoke-WASMProfiler.ps1                C:\WINDOWS\System32\WindowsPowerShell\v1.0\Invoke-WASMProfiler.ps1
    Application     http-server.py              0.0.0.0    C:\Users\ME\AppData\Local\Microsoft\WindowsApps\http-server.py
    Application     kernel32.dll                10.0.17... C:\WINDOWS\system32\kernel32.dll
    Application     user32.dll                  10.0.17... C:\WINDOWS\system32\user32.dll
    

    You can quickly set up an alias with sal which gcm (short form of set-alias which get-command).

    More information and examples can be found under the online help for Get-Command.

提交回复
热议问题