0

I'm trying to add a filter or query to a line of PowerShell code, but I am getting weird results. I can replicate this problem using any WMI query. Here is an example:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "Get-WmiObject -class Win32_Processor -Namespace root\cimv2 -filter 'Status = `"OK`"'"

That gives me this error: Get-WmiObject : Invalid query "select * from Win32_Processor where Status = OK". But if I run it in a PowerShell window that is already open, like this:

Get-WmiObject -class Win32_Processor -Namespace root\cimv2 -filter 'Status = "OK"'

That works fine. So I am not understanding this behavior. I tried every manner of single quotes, double quotes, and escape characters, but nothing seems to work.

4
  • Your syntax for the query is wrong: -Filter 'Name LIKE "Drivers - %"' Commented Jun 4, 2018 at 22:43
  • I tried it with LIKE too but it did not work. For some reason it works fine without using powershell.exe -command to start it, but not with. Commented Jun 5, 2018 at 3:09
  • Your statement didn't make any sense. How are you actually calling this? Commented Jun 5, 2018 at 3:11
  • I changed the question to a more simple example. Commented Jun 5, 2018 at 12:40

1 Answer 1

1

Your problem is a misunderstanding of quoting with the cmd parser (as that's what is used when calling powershell.exe):

powershell.exe -Command ""

Everything between the double-quotes will be passed to -Command. In your case, the easiest route to go will be escaping single-quotes (by doubling the count) since cmd handles double-quotes in a special way (usually, by removing them):

powershell.exe -Command "Get-WmiObject -Class Win32_Processor -Filter 'Status = ''OK'''"

As a footnote, root/cimv2 is the default namespace for Get-WmiObject

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

1 Comment

I think that was my problem, I didn't know you could escape single quotes by doubling them. I thought you had to use the tilde just like you do for double quotes.

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.