Search code examples
windowscommand-linepath-variables

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


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 path to a program on the Windows command line, given just its name.

Is there an equivalent to the UNIX command 'which'?

On UNIX, which command prints the full path of the given command to easily find and repair these shadowing problems.


Solution

  • I suggest you use Windows PowerShell's Get-Command cmdlet, as described in another answer to this question by Marawan Mamdouh.

    If you're like me and don't use PowerShell, use Windows' where.exe program. Like Unix which, it shows the full path that would match the filename-only command you type:

    C:\> where edit
    C:\Windows\System32\edit.com
    

    Unlike which, where.exe can show all the paths that match the given filename, though only the first one shown would normally be used when launching a program:

    C:\> where notepad
    C:\Windows\System32\notepad.exe
    C:\Windows\notepad.exe
    

    where.exe tries to follow the Windows command shell CMD.EXE's rules for what file names and paths work as commands. That includes the %PATHEXT% environment variable and the ability to open non-program data files using their filenames with extensions:

    C:\> set pathext
    PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    
    C:\> where Enterprise.xml slmgr
    C:\Windows\Enterprise.xml
    C:\Windows\System32\slmgr.vbs
    

    where.exe will even accept wildcards, so where nt*.exe finds all files in your %PATH% and current directory whose names start with nt and end with .exe:

    C:\> where nt*.exe
    C:\Windows\System32\ntoskrnl.exe
    C:\Windows\System32\ntprint.exe
    C:\Windows\System32\ntvdm.exe
    

    Check the online docs for where.exe or run where.exe /? for more help.


    Notes:

    • When using PowerShell, run where.exe as where.exe, never where. Plain where in PowerShell is an alias for the Where-Object cmdlet, which does something very different from where.exe / which.
    • where.exe is available in Windows Server 2003 and later. Sorry if you're still on Windows XP.