Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Does powershell have an equivalent to the which command found in most (if not all) unix shells?

There are a number of times I'd like to know the location of something I'm running from the command line. In unix I just do which <command> and it tells me. I can't find an equivalent in Powershell.

share|improve this question
add comment

4 Answers 4

up vote 15 down vote accepted

This was asked and answered on Stack Overflow: Equivalent of *Nix ‘which’ command in Powershell?

The very first alias I made once I started customizing my profile in powershell was 'which'.

New-Alias which get-command

To add this to your profile, type this:

"`nNew-Alias which get-command" | add-content $profile The `n is to ensure it will start as a new line.
share|improve this answer
    
Exactly what I was looking for. thanks! –  Herms Sep 2 '09 at 19:17
add comment

As of PowerShell 3.0, you can do

(Get-Command cmd).Path

Which also has the benefit over vanilla Get-Command of returning a System.String so you get a clean *nixy single line output like you may be used to. Using the gcm alias, we can take it down to 11 characters.

(gcm cmd).Path
share|improve this answer
    
If Get-Command finds multiple results, it returns an array. Additionally, if the command it finds is not an executable, Path is undefined ($null). This makes the answer here impractical for general use without heavy modification. For a good example of both these cases, try Get-Command where. –  jpmc26 May 30 at 17:14
add comment
function which([string]$cmd) {gcm -ErrorAction "SilentlyContinue" $cmd | ft Definition}
share|improve this answer
add comment

Also answered in 2008: Is there an equivalent of 'which' on windows?

Try the where command if you've installed a Resource Kit: http://ss64.com/nt/where.html

Most important parts of the answer:

Windows Server 2003 and later provide the WHERE command which does some of what which does, though it matches all types of files, not just executable commands.

[snip]

In Windows PowerShell you must type where.exe.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.