To find the equivalent of Bash's which command in PowerShell, use these methods:
1. where.exe (Windows Command-Line Tool)
Searches the system's PATH for executables. In PowerShell, use where.exe explicitly to avoid conflicts with the Where-Object cmdlet.
Example:
where.exe notepad.exe
Output:
C:\Windows\System32\notepad.exe
Note: This works for executables but not for PowerShell cmdlets or built-in commands[[1]][[2]].
2. Get-Command (PowerShell Cmdlet)
Returns the path of executables, cmdlets, functions, and aliases. Use .Path to extract the path directly.
Example:
(Get-Command winget).Path
Output:
C:\Program Files\WindowsApps\Microsoft.DesktopAppInstaller_1.21.4012.0_x64__8wekyb3d8bbwe\winget.exe
Note: This is more versatile but requires object manipulation[[1]][[3]].
3. Alias for Simplicity
Create a custom alias to mimic which:
Set-Alias -Name which -Value Get-Command
Then use:
which winget | Select-Object -ExpandProperty Path
Comparison Table
| Method | Use Case | Syntax Example |
|---|
where.exe | Find executables in PATH | where.exe cmd |
Get-Command | Find cmdlets, functions, aliases | (Get-Command cmd).Path |
| Custom Alias | Simplify syntax | which cmd (after aliasing) |
Key Differences
where.exe: Limited to executables, returns strings[[1]][[2]].
Get-Command: Handles PowerShell-specific commands and returns objects[[3]][[7]].
- Built-in Commands: Use
Get-Command for cmdlets like Get-ChildItem or Write-Host[[3]][[7]].
For most users, where.exe suffices for executables, while Get-Command is better for PowerShell-specific workflows[[1]][[3]][[7]].