0

This works in command prompt:

where Java

In PowerShell where is an alias for Where-Object. I tried to stop parsing:

--% where Java

This does not work, also tried storing as a variable:

$StopParser = "--%"
$StopParser where Java

This returns the error: 'Unexpected token 'where' in expression or statement'.

How can I use the where command in PowerShell?

0

1 Answer 1

2

This isn't a question of parsing, but of command origins. The where cmd command is an executable in System32 that searches your current directory and the PATH environment variable for a pattern:

WHERE Java

is equivalent to:

cmd.exe /C where.exe Java

The link I provided shows an equivalent command for powershell:

(Get-Command -Name $file).Definition

or you can alternatively call the executable the same way:

where.exe Java

For quite a few cmd tools, this is the same paradigm: an executable in System32.

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

2 Comments

It's not internal (under Windows 7, at least) [COPY and DIR are internal in the sense that they are code within cmd.exe]; it's implemented as C:\Windows\System32\where.exe. It can be invoked from within PowerShell as where.exe, explicitly including the file extension to override the internal alias.
WHERE Java isn't quite the same as cmd.exe /C where.exe Java, although the practical effect/output is the same. (They're not precisely equivalent because you're spinning up a copy [unnecessarily] of cmd.exe.) It's more accurate to say that WHERE Java is equivalent to where.exe Java (as you note at the end of your answer).

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.