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

后端 未结 26 2034
悲哀的现实
悲哀的现实 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条回答
  • 2020-11-22 00:53

    In PowerShell, it is gcm, which gives formatted information about other commands. If you want to retrieve only path to executable, use .Source.

    For instance: gcm git or (gcm git).Source

    Tidbits:

    • Available for Windows XP.
    • Available since PowerShell 1.0.
    • gcm is an alias of Get-Command cmdlet.
    • Without any parameters, it lists down all the available commands offered by the host shell.
    • You can create a custom alias with Set-Alias which gcm and use it like: (which git).Source.
    • Official docs: https://technet.microsoft.com/en-us/library/ee176842.aspx
    0 讨论(0)
  • 2020-11-22 00:53

    Here is a function which I made to find executable similar to the Unix command 'WHICH`

    app_path_func.cmd:

    @ECHO OFF
    CLS
    
    FOR /F "skip=2 tokens=1,2* USEBACKQ" %%N IN (`reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\%~1" /t REG_SZ  /v "Path"`) DO (
     IF /I "%%N" == "Path" (
      SET wherepath=%%P%~1
      GoTo Found
     )
    )
    
    FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe %~1`) DO (
     SET wherepath=%%F
     GoTo Found
    )
    
    FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES%" %~1`) DO (
     SET wherepath=%%F
     GoTo Found
    )
    
    FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES(x86)%" %~1`) DO (
     SET wherepath=%%F
     GoTo Found
    )
    
    FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%WINDIR%" %~1`) DO (
     SET wherepath=%%F
     GoTo Found
    )
    
    :Found
    SET %2=%wherepath%
    :End
    

    Test:

    @ECHO OFF
    CLS
    
    CALL "app_path_func.cmd" WINWORD.EXE PROGPATH
    ECHO %PROGPATH%
    
    PAUSE
    

    Result:

    C:\Program Files (x86)\Microsoft Office\Office15\
    Press any key to continue . . .
    

    https://www.freesoftwareservers.com/display/FREES/Find+Executable+via+Batch+-+Microsoft+Office+Example+-+WINWORD+-+Find+Microsoft+Office+Path

    0 讨论(0)
  • 2020-11-22 00:54

    I have a function in my PowerShell profile named 'which'

    function which {
        get-command $args[0]| format-list
    }
    

    Here's what the output looks like:

    PS C:\Users\fez> which python
    
    
    Name            : python.exe
    CommandType     : Application
    Definition      : C:\Python27\python.exe
    Extension       : .exe
    Path            : C:\Python27\python.exe
    FileVersionInfo : File:             C:\Python27\python.exe
                      InternalName:
                      OriginalFilename:
                      FileVersion:
                      FileDescription:
                      Product:
                      ProductVersion:
                      Debug:            False
                      Patched:          False
                      PreRelease:       False
                      PrivateBuild:     False
                      SpecialBuild:     False
                      Language:
    
    0 讨论(0)
  • 2020-11-22 00:54

    Go get unxutils from here: http://sourceforge.net/projects/unxutils/

    gold on windows platforms, puts all the nice unix utilities on a standard windows DOS. Been using it for years.

    It has a 'which' included. Note that it's case sensitive though.

    NB: to install it explode the zip somewhere and add ...\UnxUtils\usr\local\wbin\ to your system path env variable.

    0 讨论(0)
  • 2020-11-22 00:55

    If you have PowerShell installed (which I recommend), you can use the following command as a rough equivalent (substitute programName for your executable's name):

    ($Env:Path).Split(";") | Get-ChildItem -filter programName*
    

    More is here: My Manwich! PowerShell Which

    0 讨论(0)
  • 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.

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