5

I'm wanting the same functionality within PowerShell that where.exe gives me.

I've tried to find a cmdlet alternative to the standard where executable but can't find anything.

So by default, I resulted into trying to execute the where command within PowerShell but there is an Alias mapped to that command.

PS C:\> Get-Command where

CommandType     Name                                           Version    Source
-----------     ----                                           -------    ------
Alias           where -> Where-Object

Is there a way to execute where within PowerShell or is there a better way to achieve the same result?

4
  • 1
    Get-Command notepad -CommandType Application Commented Oct 9, 2017 at 9:15
  • Get-Command doesn't work for searching Commented Oct 9, 2017 at 9:21
  • 1
    You can execute where.exe (or any executable) within powershell like so: & where.exe, e.g. & where.exe /R C:\Windows notepad.exe Commented Dec 11, 2017 at 12:03
  • @GarethLyons I do not think the & character is need in a case like this. Commented Dec 11, 2017 at 13:41

3 Answers 3

10

Of course, if you do not wish to remove the where alias which points to Where-Object, you can type .exe (typing . followed by the TAB key is enough), as in:

where.exe notepad

Otherwise, have you tried Get-Command (alias gcm) with the -All parameter (PowerShell 3 and later)? For example:

gcm notepad -All

on my machine, gives:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     notepad.exe                                        10.0.10... C:\Windows\system32\notepad.exe
Application     notepad.exe                                        10.0.10... C:\Windows\notepad.exe

And it will find PowerShell things as well, as in the example gcm where -All which gives:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           where -> Where-Object
Application     where.exe                                          10.0.10... C:\Windows\system32\where.exe

If you do want to remove the "standard" alias, use Remove-Item alias:where (can by put in a profile file if you want it done at each start-up of PowerShell).

Sign up to request clarification or add additional context in comments.

1 Comment

:thumbsup: for noting that you can execute where by including the .exe extension
5

I would not remove the existing where alias since this would cause scripts you might use to fail. Instead you could define a new alias for it:

Set-Alias -Name "wherecli" -Value "where.exe"

And then you can use wherecli.

3 Comments

Ah, yeah good workaround! Is it possible to just set an Alias for a given session? Thanks
The cmdlet sets the alias only for the current session ;-)
Just seen that, Doh!
2

I've found a workaround with just the Get-ChildItem and Where-Object but this seems slower than where.exe but thought I'd list it as depending what you're searching it might be useful.

PS C:\> Get-ChildItem -Path "c:\Windows\" -Recurse -File | Where-Object {$_.Name -eq "notepad.exe"}

1 Comment

Using the Filter param should be quicker than piping to where-object: Get-ChildItem -Path "c:\Windows\" -Recurse -File -Filter 'notepad.exe' Although not sure how it compares to where.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.