Finding files in the PATH with PowerShell

Just a quick post this time!

I frequently find myself wanting to know where a command in my PATH is. Earlier today it was to work out why DNVM beta4 was being loaded despite installing DNVM beta6. (It was the installation from Visual Studio!). For this I wanted to find out which folder in the path had the dnvm.ps1 file.

This is pretty quick with PowerShell, but seemed like a handy thing to put on my blog for future reference Smile

The main part of the script is the “$matches = ….” line. This splits the PATH variable and pipes each folder joining it with the filename that you passed in. Then filters out any paths that don’t exist to leave you with a list of paths that existed for that filename. The first item in the list (if there are any matches) is the one that will be used when executing the command.

param (
    [string] $filename

$matches = $env:Path.Split(';') | %{ join-path $_ $filename} | ?{ test-path $_ }

if ($matches.Length -eq 0){
    "No matches found"
} else {

Usage is simply: Find-InPath somefile.ps1

Grab it here: Find-InPath.ps1

Skip to main content