Home > Software > PowerShell’s Which Equivalent: Finding Commands Across Windows and Unix Systems

PowerShell’s Which Equivalent: Finding Commands Across Windows and Unix Systems

Anastasios Antoniadis

Share on X (Twitter) Share on Facebook Share on Pinterest Share on LinkedInPowerShell, a task automation and configuration management framework from Microsoft, has become an indispensable tool for system administrators and developers alike, especially those working in Windows environments. One common task across all operating systems is the need to locate the executable file associated …

Powershell

PowerShell, a task automation and configuration management framework from Microsoft, has become an indispensable tool for system administrators and developers alike, especially those working in Windows environments. One common task across all operating systems is the need to locate the executable file associated with a command. In Unix-like systems, the which command is a widely used utility for this purpose. However, PowerShell does not have a built-in which command, given its roots in the Windows ecosystem. This article explores the equivalents of the Unix which command in PowerShell, offering insights into finding commands and their associated executable paths.

The Unix which Command

In Unix-like operating systems, which is a command-line utility that shows the full path of the shell commands. For example, typing which python in the terminal might return /usr/bin/python, indicating the path where the Python interpreter is located. This utility is particularly useful for understanding which version of a program will be executed by default when invoked from the shell, especially in environments where multiple versions of the same program might be installed.

Finding Command Paths in PowerShell

PowerShell provides several ways to achieve the functionality provided by the Unix which command. Below are the most common methods:

Method 1: Using the Get-Command Cmdlet

The Get-Command cmdlet is the closest PowerShell equivalent to Unix’s which. It retrieves all commands, including cmdlets, functions, workflows, aliases, and executable files, that are installed on the system.

Get-Command python

This command will return an object that includes the path to the python executable, among other details. To get just the path, you can select the Source property:

(Get-Command python).Source

Method 2: The where.exe Utility on Windows

Windows provides a command-line utility named where (or where.exe), which performs a similar function to the Unix which. It searches for executable files in the directories listed in the system’s PATH environment variable. Since where is an external command, it can be invoked directly from PowerShell.

Example:

where python

This command will print the path to the python executable if it exists in any of the directories listed in your PATH.

Method 3: Creating an Alias for Get-Command

For those who frequently switch between Unix-like systems and Windows, or for anyone who prefers the succinctness of the which command, you can create an alias in PowerShell that maps which to Get-Command for a more Unix-like experience.

Example:

function which { Get-Command $args[0] | Select-Object -ExpandProperty Source }
Set-Alias -Name which -Value which

After defining this function and alias in your PowerShell profile, you can simply use which python to get the path to the Python executable, mimicking the Unix behavior.

Method 4: Leveraging PowerShell Core on Unix Systems

With the advent of PowerShell Core, PowerShell is now available on Unix systems, providing a cross-platform scripting solution. On these systems, PowerShell Core respects Unix conventions and can directly utilize native commands like which. However, for consistent cross-platform scripting, it’s advisable to use PowerShell cmdlets like Get-Command.

Conclusion

While PowerShell does not include a direct equivalent to the Unix which command out of the box, it offers versatile cmdlets like Get-Command and the ability to utilize external utilities like where.exe to locate executable files. By understanding these methods and potentially creating custom aliases, PowerShell users can easily find the paths of commands across different systems, enhancing cross-platform compatibility and scripting flexibility. Whether you’re a system administrator, developer, or IT professional, mastering these techniques in PowerShell ensures you can seamlessly transition between Unix-like and Windows environments, leveraging the best tools each has to offer.

Anastasios Antoniadis
Follow me
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x