1

I am trying to get an output from a WMIC command to set as variable, For some reason it does not work. Can someone assist. This is what I am trying to do

for /f "tokens=*" %q IN (' WMIC /Node^:Comp1 PATH Win32_UserProfile WHERE LocalPath="C:\\users\\ABC1" GET Status ^| find /V "Status" ') do set pat= %q

Is there something wrong that I am doing?

  • C:\Windows\System32>for /F "tokens=*" %q in (' WMIC /node:Comp1 PATH win32_UserProfile WHERE LocalPath="C:\\users\\ABC1" GET Status ^| Find /V "Status" ') do set pro= %q Invalid Verb. – NirmalKD Mar 5 '18 at 23:58
  • Managed to get it working with this command – NirmalKD Mar 6 '18 at 3:59
  • C:\Windows\System32>for /F "tokens=*" %a IN (' WMIC /node:Comp1 PATH win32_UserProfile WHERE "LocalPath='C:\\users\\ABC1'" GET Status ^| Find /V "Status" ^| Find /V "" ') do set prop=%a :\Windows\System32>set prop=0 :\Windows\System32>set prop= – NirmalKD Mar 6 '18 at 3:59
  • Any Idea how I can remove all the Spaces from the output of this command – NirmalKD Mar 6 '18 at 4:00
  • I have tried googling it and found that most answers are a little complicated. – NirmalKD Mar 6 '18 at 4:00
1

The command executed by FOR /F goes through extra parsing that converts all unquoted/unescaped cmd.exe token delimiters into spaces. So WHERE LocalPath="value" becomes
WHERE LocalPath "value".

You can escape the =

for /f "tokens=*" %q IN (
  'WMIC /Node^:Comp1 PATH Win32_UserProfile WHERE LocalPath^="C:\\users\\ABC1" GET Status ^| find /V "Status" '
) do set pat= %q

Or you can enclose the entire WHERE clause in double quotes and then use single quotes for the value (this is my preferred way to write WHERE clauses with WMIC when used with FOR /F)

for /f "tokens=*" %q IN (
  'WMIC /Node^:Comp1 PATH Win32_UserProfile WHERE "LocalPath='C:\\users\\ABC1'" GET Status ^| find /V "Status" '
) do set pat= %q

I don't think the : really needs to be escaped, but I don't see how it can do any harm either.

You can run into the same problem when selecting multiple values with WMIC in a for /F - the unquoted/unescaped commas are turned into spaces. But in these cases quoting is not an option - you must escape the commas.

Something like this will not work:

for /f "delims=" %%A in ('wmic ...... get value1,value2,value3') do ...

You must escape the commas:

for /f "delims=" %%A in ('wmic ...... bet value1^,value2^,value3') do ...

EDIT

Well actually, there is a trick that sometimes allows you to completely eliminate all escaping. Since the command executed by FOR /F is executed via CMD /C, you can take advantage of the fact that CMD /C will strip enclosing double quotes.

So something like the following will work without any escapes as long as the keyword doesn't have any characters that need escaping (it isn't quoted during the first round of parsing)

for /f "delims=" %%A in (
  '"wmic .... where this='x' and that='y' get value1,value2,value3 | find "keyword"'
) do ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.