Is there a Windows command line equivalent to the UNIX ‘which’ command?
I sometimes encounter path problems where one of my own cmd scripts is hidden (shadowed) by another program that appears earlier in the PATH. I need a way to find the full path to a program on the Windows command line when given only its name.
Is there a Windows equivalent to the UNIX ‘which’ command?
On UNIX systems, the command which command prints the full path of the given command, making it easy to identify and resolve these shadowing issues.
Yes, Windows has several command line equivalents to the UNIX ‘which’ command, including the built-in where command and PowerShell’s Get-Command. These tools help you find the full path to executables and resolve PATH shadowing issues by showing which version of a command will be executed based on the order of directories in your PATH environment variable.
Contents
- Windows Command Line Equivalents
- Using the
whereCommand - PowerShell Solutions
- Alternative Methods
- Practical Examples for PATH Debugging
- Creating Your Own ‘which’ Equivalent
Windows Command Line Equivalents
Windows offers multiple ways to find executable paths, each with different capabilities and use cases. The most direct equivalents to UNIX’s which are:
where command - The closest native Windows equivalent that searches through PATH directories and shows all matches in order of execution priority.
PowerShell’s Get-Command - More powerful and informative, providing extensive details about commands including their source, version, and path.
whereis - While not a built-in Windows command, some third-party tools or Unix-like environments (like Git Bash or WSL) may provide this functionality.
The primary difference between these tools is that where shows you what Windows would actually execute based on PATH order, while PowerShell’s Get-Command provides more comprehensive information about available commands.
Using the where Command
The where command is Windows’ built-in solution for finding executable paths and is the most direct equivalent to UNIX’s which. It searches through the directories specified in your PATH environment variable and displays all matches.
Basic Syntax
where [options] <command_name>
Common Options
/R <directory>- Search recursively in the specified directory/F- Force exact match (no wildcard expansion)/T- Show file sizes and last modified dates
Examples
where python # Find all Python executables in PATH
where /R C:\ python # Recursively search C:\ for python.exe
where notepad # Find notepad.exe location
where ping # Show which ping command will be used
The where command will display results in the order they would be executed by the system, with the first match being the one that would run if you simply typed the command name.
PowerShell Solutions
PowerShell provides even more powerful alternatives for finding executable paths with additional metadata and filtering capabilities.
Get-Command
The primary PowerShell equivalent to which is Get-Command, which provides comprehensive information about commands.
Get-Command python # Find Python commands
Get-Command -Name python # Explicit parameter name
Get-Command -Name ping -All # Show all matches, not just the first
Get-Command notepad | Select-Object Name, Source, Definition
Get-Command with Additional Properties
Get-Command python | Format-List Name, Source, Version, Definition
Get-Command ping | Where-Object { $_.CommandType -eq "Application" }
PowerShell Aliases for Quick Use
# Create an alias similar to UNIX which
Set-Alias which Get-Command
# Usage
which python
which -All ping
PowerShell’s Get-Command is more powerful than where as it can find not only executables but also cmdlets, functions, aliases, and scripts, plus it provides metadata like version information.
Alternative Methods
When built-in commands aren’t available or you need different approaches, several alternatives can help you find executable paths.
Using for Loop with Environment Variables
@echo off
for %%i in (python.exe) do echo %%~$PATH:i
This command expands to the full path of python.exe if found in PATH, or shows nothing if not found.
Registry-Based Solutions
Windows stores some application paths in the registry, which you can query:
reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe
Manual PATH Search
You can manually search through PATH directories:
for %%d in (%PATH%) do if exist "%%d\python.exe" echo Found: %%d\python.exe
Third-Party Tools
- GnuWin32 Coreutils - Provides UNIX-like tools including
which - Git Bash - Includes
whichcommand from the UNIX toolset - WSL (Windows Subsystem for Linux) - Full UNIX environment with native
which
Practical Examples for PATH Debugging
Here are practical solutions for the specific problem of identifying shadowed commands and understanding PATH resolution.
Finding the Executed Version of a Command
where python
This shows all Python executables in PATH order, helping you identify which one would actually run.
Creating a PATH Order Map
@echo off
echo Current PATH directories:
for %%d in (%PATH%) do echo %%d
echo.
echo Where commands are found:
where ping
where python
where node
Identifying Shadowed Scripts
If your own script is being hidden by system commands, use this approach:
@echo off
setlocal enabledelayedexpansion
:: Search for a specific command
set command=myscript
echo Searching for %command%...
:: Check each PATH directory
for %%d in (%PATH%) do (
if exist "%%d\%command%.bat" (
echo Found: %%d\%command%.bat
echo This would be executed instead of your script
)
)
:: Show what Windows would actually run
where %command%
PowerShell PATH Analysis
# Get PATH as array
$paths = $env:PATH -split ';'
# Find all instances of a command
$command = "python"
$results = @()
foreach ($path in $paths) {
$fullPath = Join-Path $path "$command.exe"
if (Test-Path $fullPath) {
$results += [PSCustomObject]@{
Path = $path
FullPath = $fullPath
Size = (Get-Item $fullPath).Length
Modified = (Get-Item $fullPath).LastWriteTime
}
}
}
# Display results
$results | Format-Table -AutoSize
Write-Host "Command '$command' would execute:" $results[0].FullPath
Creating Your Own ‘which’ Equivalent
For more customized solutions, you can create batch scripts or PowerShell functions that provide exactly the functionality you need.
Batch Script ‘which.bat’
@echo off
if "%~1"=="" (
echo Usage: which ^<command^>
echo Finds the full path of a command in PATH
exit /b 1
)
setlocal
set command=%~1
:: Check if command exists in PATH
for %%i in ("%command%") do set fullpath=%%~$PATH:i
if defined fullpath (
echo %fullpath%
) else (
echo Command '%command%' not found in PATH
exit /b 1
)
PowerShell Function ‘Which-Command’
function Which-Command {
param(
[Parameter(Mandatory=$true)]
[string]$Name,
[switch]$All
)
if ($All) {
Get-Command -Name $Name -All | Select-Object Name, Source, Definition
} else {
$result = Get-Command -Name $Name -ErrorAction SilentlyContinue
if ($result) {
$result.Definition
} else {
Write-Warning "Command '$Name' not found"
}
}
}
# Set alias for easy use
Set-Alias which Which-Command
Enhanced Version with PATH Information
function Show-CommandPath {
param(
[Parameter(Mandatory=$true)]
[string[]]$Commands
)
$paths = $env:PATH -split ';'
foreach ($cmd in $Commands) {
Write-Host "`nCommand: $cmd" -ForegroundColor Cyan
Write-Host "========================"
$found = $false
foreach ($path in $paths) {
$fullPath = Join-Path $path "$cmd.exe"
if (Test-Path $fullPath) {
$found = $true
$item = Get-Item $fullPath
Write-Host "Found in: $path" -ForegroundColor Green
Write-Host " Full path: $($item.FullName)"
Write-Host " Size: $([math]::Round($item.Length / 1KB, 2)) KB"
Write-Host " Modified: $($item.LastWriteTime)"
}
}
if (-not $found) {
Write-Host "Command '$cmd' not found in PATH" -ForegroundColor Red
}
}
}
Conclusion
Windows provides several effective alternatives to the UNIX ‘which’ command, with the where command being the most direct equivalent and PowerShell’s Get-Command offering the most comprehensive functionality. For resolving PATH shadowing issues, these tools allow you to see exactly which version of a command will be executed and understand the order of directories in your PATH environment variable.
Key recommendations:
- Use
where commandfor quick, native Windows solutions - Use PowerShell’s
Get-Commandfor detailed command information and metadata - Create custom scripts for repeated PATH analysis tasks
- Consider third-party Unix-like tools if you frequently work across platforms
These tools will help you diagnose and resolve the shadowing issues you’re experiencing with your cmd scripts being hidden by other programs in your PATH.