As I sometimes have path problems, where one of my own cmd scripts is hidden (shadowed) by another program (earlier on the path), I would like to be able to find the full path to a program on the Windows command line, given just its name.
Is there an equivalent to the UNIX command 'which'?
On UNIX, which command
prints the full path of the given command to easily find and repair these shadowing problems.
While later versions of Windows have a
where
command, you can also do this with Windows XP by using the environment variable modifiers, as follows:You don't need any extra tools and it's not limited to
PATH
since you can substitute any environment variable (in the path format, of course) that you wish to use.And, if you want one that can handle all the extensions in PATHEXT (as Windows itself does), this one does the trick:
It actually returns all possibilities but you can tweak it quite easily for specific search rules.
Under PowerShell,
Get-Command
will find executables anywhere in$Env:PATH
.It also finds PowerShell cmdlets, functions, aliases, files with custom executables extensions via
$Env:PATHEXT
, etc. defined for the current shell (quite akin to Bash'stype -a foo
) - making it a better go-to than other tools likewhere.exe
,which.exe
, etc which are unaware of these PowerShell commands.You can quickly set up an alias with
sal which gcm
(short form ofset-alias which get-command
).In Windows PowerShell:
I have a function in my PowerShell profile named 'which'
Here's what the output looks like:
In Windows CMD
which
callswhere
:In PowerShell, it is
gcm
, which gives formatted information about other commands. If you want to retrieve only path to executable, use.Source
.For instance:
gcm git
or(gcm git).Source
Tidbits:
gcm
is an alias ofGet-Command
cmdlet.Set-Alias which gcm
and use it like:(which git).Source
.