Windows OS

Windows Equivalent of the Linux “Which” Command and How to Use It

In Linux, the “which” command is used to locate the location of an executable file/script. In Windows, there exists a similar command that essentially performs the same task. In this guide, we will showcase the usage of the “where” command in Windows, an equivalent of the “which” command in Linux.

Prerequisites:

To perform the steps that are demonstrated in this guide, you need the following components:

The “Where” Command on Windows

In Linux, whenever we invoke the “which” command, it looks for the name of the executable binary/script in the current directory and the locations described by the PATH environment variable. If a match or more is found, the “which” command can print the location of each of them on the screen, one line per match.

The “where” command on Windows functions similarly. When invoked, the “where” command looks for the name of the file in the current directory and locations that are described by the PATH environment variable. If any match is found, it prints the location of the file.

Check out all the available command options of the “where” command:

$ where /?

Note that the “which” command works with executable files only, whereas the “where” command can also look for non-executable files. In that regard, the “where” command can be described as an amalgamation of both the “which” and “find” commands in Linux.

Using the “Where” Command

The “where” command is available under the “Command Prompt”.

If you’re using the PowerShell console, launch a “Command Prompt” shell using the following command:

$ cmd

1. Basic Usage

We can use the “where” command to find the location of an EXE file:

$ where where

$ where dxdiag.exe

It can also search for DLL files:

$ where msvcr100.dll

It can also look for other files:

$ where secpol.msc

$ where X_80.png

2. Wildcards

The “where” command can also process the wildcard expressions like asterisks (*). In the following example, we’re inquiring about all the PNG files:

$ where *.png

3. Searching in a Specific Directory

By default, the “where” command looks for files in the current directory and locations that are described in the PATH variable. However, we can instruct where to look for the desired file in a completely different location.

To search in a specific directory, use the following command structure:

$ where <location>:<pattern>

For example, the following command searches for any file with the “debian” name on the drive Z:

$ where Z:\:debian*

Instead of a specific location, we can also use the other environment variables. For example, the following command prints all the files in the location that is specified in USERPROFILE:

$ where $USERPROFILE:*.*

The next example can be described as the equivalent of a basic “where” command:

$ where $PATH:*.png

4. Searching in Multiple Directories

So far, we’ve only instructed where to search in a single location. However, we can specify multiple locations to search in.

$ where <location_1>;<location_2><location_N>:<pattern>

5. File Locations with Details

By default, the “where” command prints the matching file names only. However, we can get additional details about the matching results like file size, modification time, etc.

To get more details about the matching file names, use the following command:

$ where /T *.png

6. Recursive Search

By default, the “where” command doesn’t perform the recursive searches. It only looks for the pattern that is specified on the surface level. However, it comes with the capability for recursive searching.

For recursive searches, the command structure is as follows:

$ where /r <start_dir> <pattern>

Here:

  • /r: It specifies a recursive search.
  • <start_dir>: The starting location that the “where” command uses as a starting point.
  • <pattern>: The “where” command looks for the pattern that is specified at each level.

The search stops once it has looked into all the directories and sub-directories.

In the following example, we look for ISO files in the Z drive:

$ where /r Z: *.iso

7. Double-Quoting the File Names

The “where” command can also put the matching file names in double quotes. It can be useful if you intend to use the output in other programs/scripts.

To surround the file names in double quotes, use the following command structure:

$ where /F <pattern>

$ where /F <location>:<pattern>

$ where /F /r <start_dir>:<pattern>

8. Exit Code

Depending on the success of the operation, the “where” command returns an exit code according to the following instances:

    • If the operation is successful, the exit code is 0.
    • If the search is unsuccessful, the exit code is 1.
    • If any error is encountered, the exit code is 2.

Conclusion

In this guide, we discussed about the “where” command, the Windows equivalent of the “which” command in Linux. We showcased how it works with various demonstrations. We learned about modifying the output to be used in scripts. We also learned about the exit codes to determine if the “where” command is successful or not.

Speaking of scripting, Windows 10/11 comes with PowerShell, a powerful shell program that comes with its own scripting. Check out the PowerShell sub-category to learn more about Windows PowerShell.

Happy computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.