Equivalent of *Nix 'which' command in PowerShell? Equivalent of *Nix 'which' command in PowerShell? powershell powershell

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.


Here is an actual *nix equivalent, i.e. it gives *nix-style output.

Get-Command <your command> | Select-Object -ExpandProperty Definition

Just replace with whatever you're looking for.

PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty DefinitionC:\Windows\system32\notepad.exe

When you add it to your profile, you will want to use a function rather than an alias because you can't use aliases with pipes:

function which($name){    Get-Command $name | Select-Object -ExpandProperty Definition}

Now, when you reload your profile you can do this:

PS C:\> which notepadC:\Windows\system32\notepad.exe


I usually just type:

gcm notepad

or

gcm note*

gcm is the default alias for Get-Command.

On my system, gcm note* outputs:

[27] ยป gcm note*CommandType     Name                                                     Definition-----------     ----                                                     ----------Application     notepad.exe                                              C:\WINDOWS\notepad.exeApplication     notepad.exe                                              C:\WINDOWS\system32\notepad.exeApplication     Notepad2.exe                                             C:\Utils\Notepad2.exeApplication     Notepad2.ini                                             C:\Utils\Notepad2.ini

You get the directory and the command that matches what you're looking for.