288

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?

1

6 Answers 6

298

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
2
  • 40
    If 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
    Commented Nov 18, 2013 at 20:38
  • Source works the same, see answer below Commented Jul 16, 2020 at 13:43
76

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...
1
  • Can verify it still works as of pwsh 7. Commented Apr 9 at 15:19
71

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:\>
1
  • 6
    (gcm some_program -All).Path replicates the default behavior of where.exe
    – Marduk
    Commented Nov 5, 2020 at 15:40
8

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:\>
2
  • 1
    To set a permanent alias, add it to your PowerShell profile.
    – Sojimaxi
    Commented Nov 1, 2022 at 4:08
  • 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
    Commented Feb 17, 2023 at 18:20
7

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
}
0
6

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

You must log in to answer this question.