Equivalent of *Nix 'which' command in Powershell?

Does anyone know how to ask powershell where something is?
For instance "which notepad" and it returns the directory where the notepad.exe is run from according to the current paths.


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.


Here is an actual *nix equivalent, ie 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 Definition
C:Windowssystem32notepad.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 notepad
C:Windowssystem32notepad.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:WINDOWSnotepad.exe
Application     notepad.exe                                              C:WINDOWSsystem32notepad.exe
Application     Notepad2.exe                                             C:UtilsNotepad2.exe
Application     Notepad2.ini                                             C:UtilsNotepad2.ini

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

链接地址: http://www.djcxy.com/p/30238.html

上一篇: PowerShell:以管理员身份运行命令

下一篇: Powershell中命令* Nix'的等价物?