I can't seem to find anything about a Powershell equivalent of the where command from cmd. Should I just call it from cmd or is there something more elegant in PS?
-
1Interesting reading I found sometime back on Where.exe and Get-ChildItem: https://blogs.technet.com/b/heyscriptingguy/archive/2010/07/24/weekend-scripter-where-exe-the-what-why-and-how.aspx?Redirected=true – Nov 13 '13 at 02:16
6 Answers
Use the Get-Command commandlet passing it the name of the executable. It populates the Path property of the returned object (of type ApplicationInfo) with the fully resolved path to the executable.
# ~> (get-command notepad.exe).Path
C:\WINDOWS\system32\notepad.exe
- 3,193
- 3
- 21
- 42
- 5,096
- 1
- 23
- 17
-
36If you find yourself using this a lot, you can abbreviate the command as `gcm` instead of typing the whole `Get-Command` word every time – Moshe Katz Nov 18 '13 at 20:38
-
`Source` works the same, see [answer below](https://superuser.com/a/1569151/245595) – sancho.s ReinstateMonicaCellio Jul 16 '20 at 13:43
If you're just looking to have the same functionality without invoking cmd, you can call where.exe from powershell, as long as C:\Windows\System32 is in your path. The command where (without the .exe) is aliased to Where-Object, so just specify the full name.
PS C:\Users\alec> where
cmdlet Where-Object at command pipeline position 1
...
PS C:\Users\alec> where.exe
The syntax of this command is:
WHERE [/R dir] [/Q] [/F] [/T] pattern...
- 801
- 5
- 6
where isn't a built in cmd command. It's a standalone application (where.exe), so strictly speaking PowerShell doesn't "need a replacement".
So why doesn't where work in PowerShell? It seems to do nothing:
PS C:\> where where
PS C:\>
By default where is aliased to a built-in PS cmdlet.
PS C:\> get-help where
NAME
Where-Object
...
ALIASES
where
?
Well, that's great to know, but is there a way to avoid calling where-object when trying to call where.exe?
The answer is, yes.
Option 1
Call where.exe with extension. (This is a handy way to work around other aliasing and file-extension prioritisation issues.)
PS C:\> where.exe where
C:\Windows\System32\where.exe
Option 2
Remove the alias.
PS C:\> Remove-Item alias:\where -Force
PS C:\> where where
C:\Windows\System32\where.exe
Side Notes
zdan's answer proposes using Get-Command as an alternative. Although it's a little more verbose (even when using the default gcm alias), it has richer functionality than where.exe. If used in scripting, do pay attention to the subtle differences between the two. E.g. where.exe returns all matches, whereas Get-Command returns only the first result unless you include the optional -TotalCount parameter.
PS C:\> where.exe notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
PS C:\> (gcm notepad).Path
C:\WINDOWS\system32\notepad.exe
PS C:\> (gcm notepad -TotalCount 5).Path
C:\WINDOWS\system32\notepad.exe
C:\WINDOWS\notepad.exe
PS C:\>
And finally, if you remove the default where alias, you might also consider reassigning that as an alias to Get-Command. (But this would probably be of dubious benefit.)
PS C:\> Set-Alias where Get-Command
PS C:\> where notepad
CommandType Name Version Source
----------- ---- ------- ------
Application notepad.exe 10.0.15... C:\WINDOWS\system32\notepad.exe
PS C:\> (where notepad).Path
C:\WINDOWS\system32\notepad.exe
PS C:\>
- 661
- 5
- 5
-
5`(gcm some_program -All).Path` replicates the default behavior of `where.exe` – Marduk Nov 05 '20 at 15:40
Get-ChildItem C:\SomeDir -Recurse *.dll
That's pretty much what the old where.exe does... was there more specific functionality that you're trying to mimic?
Edit: In response to Joshua's comment... oh, you want to search your PATH environment variables too? No problem.
Foreach($_ In $Env:Path -Split ';')
{
Get-ChildItem $_ -Recurse *.dll
}
- 510
- 1
- 4
- 12
Who had this idea to define aliases so that you can not call system executables present in PATH?
Anyways, most reliable solution would be, to define an alias, I am used to:
PS H:\> Set-Alias which where.exe
PS H:\> which notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
PS H:\>
- 51
- 1
- 1
-
-
I think this is the best way to go. And you can just add the alias to the master powershell profile, which is found in the variable $profile . I use Notepad++ to edit that and add the alias at the end. Just type in Powershell command line: notepad++ $profile and add the alias there (here I too went with "which"): Set-Alias which where.exe – AlexD Feb 17 '23 at 18:20
Both Path and Source from Get-Command work the same
> (Get-Command notepad).Path
C:\windows\system32\notepad.exe
> (Get-Command notepad).Source
C:\windows\system32\notepad.exe
- 5,328
- 9
- 44
- 68