Where is the File: Using Where.exe

Posted on July 19, 2012 by Jeff Hicks in Windows Server with 0 Comments

There used to be a time where you ran commands on your computer without any fancy GUIs or icons. You typed a command and it ran. In order to simplify your life, you didn’t even have to specify the full path to the program. The operating system would search in predefined set of directories and run the first matching command. These directories were stored in an environmental variable called %PATH%.

Today, most IT Pros don’t think too much about this, nor do they when using Windows. But for those of you who run commands from a command prompt, sometimes you can run into problems. This is a two-part article: Today I’ll discuss using the command line utility Where.exe. In part two, I’ll go further and discuss using Get-FileItem to help you find the files you want.


Using Where.exe

For example, if you want to run foo.exe but there are two different versions in two different folders in %PATH%, you might end up running the wrong version. If you open a command prompt in Windows 7, you have at your disposal a nifty command line utility called Where.exe. To see how to use it, run where /? at a prompt. You can see the results in Figure 1.

Where.exe Help
Figure 1 Where.exe help
The easy way to use this utility is to specify a pattern for a filename. You can even use wildcards.

The default behavior is to display the full file name for every match. Remember, by default, Where.exe is only searching folders in %PATH%. But you can specify one or more directories.

In this command, I searched for anything that started with ‘Note’ in C:\Scripts and C:\Work. If I also wanted to search %PATH%, all I need to do is include it in my directory definition.

Using /T & /R Parameters

Where you might have duplicates, it can be helpful to check size and/or a time stamp. You can accomplish that with the /T parameter.

It looks like the first two versions are identical, but clearly the last one is different.

By default, WHERE.EXE only searches in the root of the current directory, but you can do a recursive search, albeit only for a single directory with /R.

I’ve truncated the output, but I think you get the idea. Of course, you can combine parameters as well.

One caveat is that you must put the parameters in the same order that you see them in help.

Errorlevel 0

Finally, you may also want to use WHERE.EXE to determine if a file exists somewhere either in the path or a specified computer. You don’t really need the file name. If a match is made, the command will return Errorlevel 0.

This comes in handy if you are writing a batch file script.

Here’s what it looks like in action.

Running Files with Where.exe

Lastly, if you wanted to use this command to actually run the files WHERE.EXE finds, you’ll need to be creative. The technique I have works best if you know you’ll only get a single result. First, save the result to a temporary text file.

Next, create a variable from the text file contents.

Finally, invoke the variable, which should launch the program.

If your command has spaces in it, you’ll need to have WHERE.EXE include double quotes with the /F parameter.


That’s a lot of work. For a quick file search, WHERE.EXE is terrific. But if you want to do anything else with it, you run into shortcomings of the command shell.

In Part 2, I’ll show you a command line alternative to help you find the files you’re looking for.