Prerequisites:
To perform the steps that are demonstrated in this guide, you need the following components:
- A properly configured Windows system. Check out how to install Windows 10 in a VirtualBox VM or Windows 11 on VMware Workstation Pro.
- Basic understanding of the Windows command-line interface.
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:
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:
1. Basic Usage
We can use the “where” command to find the location of an EXE file:
It can also search for DLL files:
It can also look for other files:
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:
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:
For example, the following command searches for any file with the “debian” name on the drive Z:
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:
The next example can be described as the equivalent of a basic “where” command:
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.
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:
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:
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:
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:
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!