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.
Get-Command
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.
Off course it can be refined - and probably should - but it's good enough for most situations.
Ex:
> which notepad.exe
C:\WINDOWS\system32\notepad.exe
C:\WINDOWS\notepad.exe
PS:
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.
Get-Command
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.
Ex:
> which notepad.exe
C:\WINDOWS\system32\notepad.exe
C:\WINDOWS\notepad.exe
PS:
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.
You could also just use get-command or its alias gcm.
ReplyDeletegcm notepad.exe
CommandType Name Definition
----------- ---- ----------
Application notepad.exe C:\WINDOWS\system32\notepad.exe
Application notepad.exe C:\WINDOWS\notepad.exe
ARGH!! Why does this keep happening to me?? :P
ReplyDelete