239

Is there an equivalent of the Unix whereis command in Windows?

So that I could figure out where commands I can run actually is.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Svish
  • 38,310
  • 61
  • 136
  • 181
  • I think for the sake of seacheability it would make sense to inculde `type` and `which` in the question title, along with `whereis` which I personally used only occasionally. `type` is a popular Bash built-in and `which` is a variant of `whereis` restricted to searching commands (`whereis` can also search for `man` pages and the like). – Dmitry Grigoryev Jul 29 '22 at 09:11

11 Answers11

291

The where command does what you want and goes back at least to the resource kit for Windows 98, and is included by default in Server 2003, Vista, and newer:

C:\>where csc
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe

If executed with no arguments (on Vista), it results in one of my favorite messages:

C:\>where
ERROR: The operation completed successfully.

If executing in PowerShell, be sure to include '.exe' to distinguish from any 'where' aliases or scripts along the path. ('where' is a typical alias for Where-Object.ps1)

C:\> where.exe where.exe
C:\Windows\System32\where.exe
J Burnett
  • 143
  • 6
Kevin
  • 3,776
  • 1
  • 18
  • 9
18

You can run the following PowerShell command:

gcm <command>

Get-Command

The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. Get-Command gets the commands from PowerShell modules and commands that were imported from other sessions. To get only commands that have been imported into the current session, use the ListImported parameter.

Ramhound
  • 41,734
  • 35
  • 103
  • 130
JesusIniesta
  • 323
  • 2
  • 6
  • 1
    Windows 10 doesn't recognize gcm: `"'gcm' is not recognized as an internal or external command, operable program or batch file."` – Matt Apr 11 '23 at 08:16
  • This is a powershell command, must be run from a powershell prompt, not from cmd.exe – davr May 03 '23 at 18:00
13

Please, use where command:

> where app.exe

It is the best way to achieve your goal.

You can also use PowerShell command:

> $env:path.Split(';') | gci -Filter app.exe

and expanded version looks like this:

 > $env:path.Split(';') | select -Unique | ? {$_ -and (test-path $_)} | gci -Filter app.exe
Arek Bee
  • 231
  • 2
  • 3
7

hackerish which.cmd:

@echo off
@set PATH=.;%PATH%

@rem 
@rem about:  something similar like the unix-alike-which, but with
@rem         within pure cmd
@rem 

if "%1" == "" (
    @echo Usage: 
    @echo.
    @echo   which 'cmd'
    @echo.
    @echo.if 'cmd' is not found, ERRORLEVEL is set to 1
    @echo.  
) else (
    ( @for %%f in (%1 %1.exe %1.cmd %1.bat %1.pif) do if not "%%~$PATH:f" == "" ( @echo %%~$PATH:f ) else @set ERRORLEVEL=1) 
)
akira
  • 61,009
  • 17
  • 135
  • 165
  • 1
    This is a good fix for older systems, but you should know that it results in a few quirks. It matches directories, only returns the first result found in the path for each extension, and should include every extension found in the PATHEXT environment variable. – Kevin Sep 11 '09 at 14:48
  • yah, this is a bit older hack of mine, when i pasted it here i instantly saw the potential for %PATHEXT% :) – akira Sep 11 '09 at 18:08
3

Using the where command on windows 10, in cmd, I used the following command to recursively search c drive for copies of the c# compiler, csc.exe.

c:\> where /r c:\ csc.exe

Sample:

enter image description here

Matt
  • 374
  • 3
  • 14
Simon Hooper
  • 131
  • 3
3

Somewhere "out there" I found this batch file whereis.bat:

@for %%e in (%PATHEXT%) do @for %%i in (%1%%e) do @if NOT "%%~$PATH:i"=="" echo %%~$PATH:i

Update: maybe I found it here.

Craig McQueen
  • 1,209
  • 1
  • 9
  • 19
1
function find ($string) { 
   Get-ChildItem -Path 'c:\' -Recurse -Filter $string; 
}

function whereis ($string) { 
   $superpath = "$env:Path;C:\Program Files;C:\Program Files (x86)";
   (echo $superpath).Split(';') | Get-ChildItem -Recurse -Filter $string; 
}

Example:

PS >find Mozilla.admx

    Directory: C:\Windows\PolicyDefinitions                                                                                     

Mode                LastWriteTime         Length Name                                                                           
----                -------------         ------ ----                                                                           
-a----        1/27/2016  12:22 PM          37146 Mozilla.admx                                                                   

PS >whereis firefox.exe

    Directory: C:\Program Files\Mozilla Firefox                                                                                 

Mode                LastWriteTime         Length Name                                                                           
----                -------------         ------ ----                                                                           
-a----        9/21/2017   5:30 PM         477136 firefox.exe        
Rupert
  • 31
  • 1
  • 8
1

which.cmd

@echo off

if "%~1" equ "" (
    echo usage: %~n0 [command] & exit /b 0
) else where $Path:%1 2>nul || (
    echo | set /p x=%~n0: no '%1' in & path
)

doskey.exe

doskey which=where $1 2$Gnul

Usage:

> which.cmd git
C:\Program Files\Git\cmd\git.exe

> which.cmd foo
which: no 'foo' in PATH=C:\Windows\system32;C:\Windows;C:\Win
dows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0
\;C:\Windows\System32\OpenSSH\;C:\Program Files\PowerShell\7\;
sionta
  • 21
  • 3
1

There is at least a Windows port for the which utility.

innaM
  • 10,192
  • 5
  • 42
  • 52
1

I was searching for this today and since I'm on XP without the resource kit, I turned to powershell with the following command:

dir -path c:\ -filter ffmpeg.* -r
KalenGi
  • 147
  • 2
  • 8
  • I'm not proficient in powershell but it seems you're searching through the whole directory tree. This is not equivalent to `where` which only searches in the `%PATH%`. Moreover it's much slower and gives errors for folders you don't have read permission – phuclv Apr 30 '17 at 03:07
  • Agreed... I did not require an exact copy of the functionality, just the ability to locate a program. – KalenGi Apr 30 '17 at 09:44
-1

You can try searching for the command using the following:

dir /s type-whatever-you-are-searching
phuclv
  • 26,555
  • 15
  • 113
  • 235
Shakeel
  • 7
  • 2
  • This does not work for me. For example, the exp command is in my path, but dir/s exp or dir /s exp.exe just gives "File Not Found". – bobmcn Sep 10 '09 at 21:12
  • 5
    This would work if a) you search from the root of the drive, b) your path is all on one drive, and c) your path is in lexicographical order. Even under these conditions it will be ridiculously slow. – Kevin Sep 10 '09 at 23:09