PowerShell: which for windows

As Stephen Mills points out in a comment below, all this is unnecessary because there's a cmdlet that does this even better.


So please disregard this post :P

Fresh code out of the oven. I was on my Windows XP box and needed to know where I had an specific executable file stored. Some Windows boxes have a similar command called where but it wasn't availed in my box. So I just coded one in PowerShell.

function which ($file){
$return = @()
if (Test-Path ".\$file" -ea silentlycontinue){ # check current dir
$return +=@(".\$file")
foreach ($path in (get-content env:path).Split(";")){ # check all paths in PATH env var
if (Test-Path "$path\$file" -ea silentlycontinue){
$return += @("$path\$file")
return $return

Off course it can be refined - and probably should - but it's good enough for most situations.


> which notepad.exe

I store the output in a string array instead of printing out the string because to be able to use the output as an object and easily do whatever I want with it.