34

When I run where in CMD, I get output:

C:\Users\Ragesh> where calc
C:\Windows\System32\calc.exe

But the same thing in PS:

PS C:\data\code> where calc
PS C:\data\code>

Where's the output going?!

Ragesh
  • 2,800
  • 2
  • 25
  • 36

2 Answers2

46

The following worked for me:

PS C:\Users\Bill> where.exe calc
C:\Windows\System32\calc.exe

When you type where in PS, it is not same as executing where.exe

PS C:\Users\Bill> where  <press ENTER>

cmdlet Where-Object at command pipeline position 1
Supply values for the following parameters:
Property:

So when you type where calc it is executing Where-Object calc (the alias of Where-Object is where and ?) and thus returns nothing, and not executing where.exe calc.

You can use the Get-Command (alias gcm) cmdlet instead of where.exe. Here is an example function to make Get-Command function exactly like where.exe. If you put this in your PowerShell profile it will always be available in your session.

function which ($command) {
    Get-Command -Name $command -ErrorAction SilentlyContinue | 
        Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}

The following links may be useful -

Equivalent of *Nix 'which' command in Powershell?

https://superuser.com/questions/34492/powershell-equivalent-to-unix-which-command

Hope this helps.

Community
  • 1
  • 1
Bill
  • 5,263
  • 6
  • 35
  • 50
  • 2
    Thanks for the tip. It doesn't explain why 'where' doesn't show any output in PS, though. – Ragesh May 27 '13 at 15:09
  • @Ragesh I updated the answer with explanation and a way to get this to work. Pls let me know if u have questions. – Bill May 27 '13 at 15:19
  • 2
    You can also run it in using cmd.exe. Like `cmd /c "where calc"` – Frode F. May 27 '13 at 15:25
  • @Graimer Absolutely ...pls free to edit the answer if you want. – Bill May 27 '13 at 15:25
  • 2
    You can see the command precedence that leads to this by issuing `get-command where -CommandType all`. – x0n May 27 '13 at 19:03
  • is there any way of switching off this behaviour? I found the same problem with 'sc' (for mucking around with services) and then tried to 'where' sc. Thought my computer had gone crazy! I dont want to use cmd because of the obvious horribleness but powershell randomly replacing stuff with other stuff is also awful – JonnyRaa Jan 16 '14 at 12:16
0

@Bill's answer is one reason.

Another is you're in the Powershell ISE in which case where.exe invokes a new command shell that opens, prints the output, and closes immediately.

john v kumpf
  • 431
  • 3
  • 8