Equivalent of which Command in PowerShell

Rohan Timalsina Feb 02, 2024
  1. Use Get-Command as the Equivalent of Which Command in PowerShell
  2. Use gcm as the Equivalent of Which Command in PowerShell
  3. Use New-Alias as the Equivalent of Which Command in PowerShell
Equivalent of which Command in PowerShell

In Linux, the which command displays the location of the specified executable file or command that can be executed when entered in the terminal prompt. It shows the full path of commands. The which command is not available in PowerShell.

Here is an example of the which command to view the location of gcc in Linux.

$ which gcc

Output:

/usr/bin/gcc

PowerShell has many executable files or commands that can be executed in its shell. This tutorial will introduce different methods to get those executable files or commands’ location in PowerShell. They are equivalent to the which command and perform the same task.

Use Get-Command as the Equivalent of Which Command in PowerShell

The Get-Command cmdlet displays all commands installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. It prints all the cmdlets, functions, and aliases installed on the computer when used without any parameter.

Get-Command

To view the location of gcc, you can use the command below.

Get-Command gcc

The full path is displayed on the Source column. Additionally, it shows the CommandType, Name, and Version details.

Output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     gcc.exe                                            0.0.0.0    C:\MinGW\bin\gcc.exe

You can also provide multiple arguments.

Get-Command Write-Host, New-Alias, Describe

Output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Write-Host                                         3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          New-Alias                                          3.1.0.0    Microsoft.PowerShell.Utility
Function        Describe                                           3.4.0      Pester

Use gcm as the Equivalent of Which Command in PowerShell

The gcm is a built-in alias for the Get-Command cmdlet. You can use the gcm alias as the equivalent of which command in PowerShell. It prints the same output as Get-Command.

gcm notepad

Output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     notepad.exe                                        10.0.19... C:\Windows\system32\notepad.exe

To get only the path, you can use it like this.

(gcm notepad).Path

Output:

C:\Windows\system32\notepad.exe

Use New-Alias as the Equivalent of Which Command in PowerShell

You can also define a new custom alias in PowerShell. The New-Alias cmdlet creates a new alias in the PowerShell session. Such aliases are not saved after you exit the session or close PowerShell.

For example, you can create which as an alias for the Get-Command cmdlet.

New-Alias which Get-Command

Now, you can use the which command to view the location of the executable file or command in PowerShell.

which gcc

Output:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     gcc.exe                                            0.0.0.0    C:\MinGW\bin\gcc.exe
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Command