Where command in windows | which command alternatives

In Linux, the which command is used to determine the location of an executable command in the console. However, in Windows, attempting to use this command results in an error: 'which' is not recognized as an internal or external command, operable program, or batch file.

C:\>which derby
'which' is not recognized as an internal or external command, operable program, or batch file.

What is the Windows equivalent command of the which command in Unix?

This tutorial explores alternative to which commands in Windows:

  • Using the where command
  • Cygwin tool
  • PowerShell command

Where Command in windows

The where command serves as the equivalent of the which command in Unix/Linux on Windows. It has been available since the Windows 2003 version. The where command prints the location of a file for a given search pattern within the environment variable path.

Syntax:

where  [options] searchpattern

options:-

  • /R: Recursive path for searching files
  • /F: Displays the path of the file in double quotes
  • /T: Displays file path metadata like size, modified date

Search Pattern::

It contains a string or pattern of strings using * for multiple, ? for a single character.

Here is the help documentation for the where.exe command:

C:\>where /?

Here is an example where the command for the location of the ping command

C:\>where ping
C:\Windows\System32\PING.EXE

If you want to locate the Java executable with multiple versions:

C:\>where java
A:\Java\jdk1.8.0\bin\java.exe
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe

For a search pattern starting with jav*:

where jav*

And for a search pattern jav?, it searches for words starting with jav and where the fourth letter may be any single letter or none:

where jav?

window PowerShell which alternative

In PowerShell, Get-Command works similarly to the where command, but it only functions in the PowerShell command prompt.

PS C:\> Get-Command java

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     java.exe                                           8.0.102... A:\Java\jdk1.8.0\bin\java.xe

Cygwin tool

Cygwin is a runtime tool that supports Linux commands in Windows. You can install it from here🔗.

All Linux commands, including which, work as expected in Windows with Cygwin.

Conclusion

In summary, the where command in DOS of Windows and Get-Command in PowerShell are alternatives to the which command in Windows. Additionally, Cygwin provides a runtime environment for executing Linux commands in Windows.