which command alternatives in Windows

Which command is a useful tool in Linux. If you run a command by just typing its name instead of the full path name, the shell will find the command in the directories specified in the $PATH environment variable and execute the first command it finds. This is convenient in most cases since it relieves you of typing the full path name of the command. But in some cases, it may cause problem. For example, there are multiple commands with the same name, and they are put in different directories. If you do not type the full path name of the command, the shell may execute a command that is not what you want. In this case, you can use the which or where command to check if the command to be executed is exactly what you want. Which command does the same thing as what the shell does during finding the command to executed, but instead of running the command, which command just prints the full path name of the command.

In Windows, there is no such command as which or where. But you can write a batch script to simulate the which command.

@echo %~$PATH:1

With just one line of code, you can print the full path name of a command as which does. The syntax of the batch script is strange and hard to understand. First, %1 means the second parameter when you run the script. You can name the script after  which.bat. When you use which.bat to check a command’s full path name, you run “which.bat acommand”. Then %1 is acommand. The ~$PATH: prefix will search the command in directories specified in PATH environment variable and expand %1 to the first full path name it finds. If it cannot find one in these directories, %1 will expand to empty string, then which.bat will display current echo status.


If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply