200

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.

1

11 Answers 11

146

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 at the start of the last line is to ensure it will start as a new line.

1
130

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
5
  • 5
    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
    Commented May 30, 2014 at 17:14
  • 2
    This should be the accepted answer as it actually tells you what is the Powershell equivalent of the *NIX command where rather than teaching you how to set aliases on Powershell, which is not the title of the question.
    – mastazi
    Commented Jul 12, 2015 at 23:31
  • 3
    @mastazi: But that fails for builtins, which is a regression compared to e.g. zsh's which. (where, by the way, is actually a Windows utility that can do a number of different things, one of which roughly approximates searching for a command along the PATH.) Also, there's nothing wrong with an answer that explains how to do what was asked and also another, slightly more involved thing built on that.
    – SamB
    Commented Nov 29, 2017 at 23:04
  • @SamB zsh's which isn't *nix which. The "standard" which most people use don't know about builtins at all
    – phuclv
    Commented Apr 11 at 4:33
  • Note that not all commands have a Path property; use e.g. gcm cmd | select * to see what's available.
    – c z
    Commented Aug 27 at 8:21
24

Also answered in 2008: Is there an equivalent of 'which' on the Windows command line?

Try the where command if you've installed a Resource Kit.

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.

18

I just do where.exe <command> in Powershell. This is the easiest approach that I know. The .exe part is important BTW.

3
  • gcm is much shorter than where.exe
    – phuclv
    Commented Apr 11 at 4:34
  • @phuclv but it doesn't do the same. where.exe returns a path Commented Sep 1 at 8:46
  • @HomeroEsmeraldo Get-Command returns a path. In fact it (and all powershell commands) returns an object with lots of useful properties including path. Did you even try it? Check out gcm where | select * or (gcm where).Path
    – phuclv
    Commented 2 hours ago
8
function which([string]$cmd) {gcm -ErrorAction "SilentlyContinue" $cmd | ft Definition}
8

Try this:

get-command [your command] 
4

Some great answers on this thread but I have found Powershell Alias's are a bit of a pain to re-define and functions are easier to write so I put this in my Powershell profile:

function global:which ([string]$command) {
  if (-not($command)) { throw "ERROR: Please supply a command name" }
  (Get-Command $command).Path
}
1

Get-Command doesn't find .lnk or other types, just executable commands.

To search $env:path for any file (incl. .lnk) use

@(where.exe <file pattern> 2>$null)[0]

Returns empty string if nothing found, else full path of first encounter.

1

In PowerShell 5.1 you could use

Get-Command <command> | Format-Table Name, CommandType, DisplayName, Definition, Description, Version

The path is in Definition column.

1

I know this is an old thread, but I don’t see where anyone put all the pieces together.

The best answer is dispersed within all these answers, but no one says it with the proper combo.

The built in where.exe gives you what you want (if your goal is to imitate the ‘nix version of which.)

PS aliases where to Where-Object. But that isn’t a big deal if you are a ‘nix-head, you don’t typically use where. Maybe whereis, but you are used to using which (which is not part of PowerShell as a command or an alias).

So, just alias which to where.exe. This provides the original ask and gives the look/feel of ‘nix without touching the default PS setup of where.

Also, it returns a string, so, be sure to account for multiple finds by splitting on the return character if needed.

Set-Alias -Name which -Value C:\Windows\System32\where.exe

Done.

0

Get-Command notepad to find location of a command, here notepad as an example, in Powershell.

Get-Command <your_command> in general.

If you just want the path, do (Get-Command <your_command>).Path

If you want it short do (gcm <your_command>).Path

Note: Where command does not work if not installed.

2
  • 1
    Same answer has been given many years ago.
    – Toto
    Commented Aug 12 at 9:10
  • Where in a clear form? And also indicating that where requires additional steps? BTW, (Get-Command cmd).Path is not clear, as it is not indicated which executable is searched for. Commented Aug 12 at 11:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .