WMIC output property value without property name

5

2

I'm entering something like that

Desktop>wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue
VariableValue
xxx

But I don't want VariableValue to get into output. I want simply get xxx Is it possible?

Paweł Audionysos

Posted 2017-03-24T21:16:47.843

Reputation: 209

Answers

11

I don't want VariableValue to get into output. I want simply get xxx Is it possible?

Using a batch file:

@echo off
setlocal
for /f "usebackq skip=1 tokens=*" %%i in (`wmic environment where ^(name^="PATH" and systemVariable^=FALSE^) get variableValue ^| findstr /r /v "^$"`) do echo %%i
endlocal

Using a command line:

for /f "usebackq skip=1 tokens=*" %i in (`wmic environment where ^(name^="PATH" and systemVariable^=FALSE^) get variableValue ^| findstr /r /v "^$"`) do @echo %i

Notes:

  • for /f loops through the wmic output.
  • skip=1 skips the header line (containing VariableValue)
  • findstr /r /v "^$" removes the trailing blank line from the wmic output.

Example output:

C:\Users\DavidPostill\AppData\Roaming\npm

Further Reading

DavidPostill

Posted 2017-03-24T21:16:47.843

Reputation: 118 938

Thanks. Does having line break in PATH leads to any issue or is this just cosmetic approach? – Paweł Audionysos – 2017-03-25T00:58:28.297

@PawełAudionysos I'm not sure what you mean. What are you going to do with the variable? – DavidPostill – 2017-03-25T08:57:19.853

The final goal would be to append some new path to existing PATH variable, but I would probably need to ask few more questions... The solutions for this that can be found already are not 100% correct. – Paweł Audionysos – 2017-03-25T17:08:21.407

@PawełAudionysos Really? I'm pretty sure the canonical answer is correct - What are PATH and other environment variables, and how can I set or use them?

– DavidPostill – 2017-03-25T17:10:13.983

Yea, for example there is not mention that setx PATH "xyz%PATH%" will truncate your variable to 1024 characters. Or there is mention about %PATH% returning combined both, user PATH and system pathvariable, but there is no mention how to solve that issue (for example the method in my question)... – Paweł Audionysos – 2017-03-25T17:29:36.930

@PawełAudionysos Well the 1024 characters is incorrect (windows 7 or newer) as I have explained in detail in my answer Why does Windows have a limit on environment variables at all?

– DavidPostill – 2017-03-25T17:36:21.223

I didn't count it, I'm just referring to output message which say 1024 and observation that text is truncated. As I've tested to set it with wmic the string is not truncated but It's good you linked this answer. I would be probably surprise again sooner or later as total 32K characters to be shared for all environmental variables seems quite small... Well I would not be surprised that something go wrong when trying to set 100MB file to some variable but this setx truncation happens with real scenario PATH length... – Paweł Audionysos – 2017-03-25T18:22:43.763

2

Pipe it through find:

wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue | find /i "c:"

Alternatively, you can pipe it through findstr:

wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue | findstr/n ^^|findstr "^[2-9]:"

This will give you the 2-9 lines of output. Note, however, that it will be numbered.

Ploni

Posted 2017-03-24T21:16:47.843

Reputation: 560

How does that suppose to work? – Paweł Audionysos – 2017-03-24T21:36:47.843

@PawełAudionysos When you pipe your command through find, it looks for any lines/strings containing your search. In this case I had it search for "c:" because (assuming your OS is installed on drive C:) it is bound to be somewhere in the string that you want to see, but not the header which you wanted to remove – Ploni – 2017-03-24T21:40:05.490

Ok, I thought at fist time it's some kind of nasty joke... But, is path variable limited only to system drive? Well I don't have anything on other drives but when I would have "D:" it wont display it if I understand it right. Besides this is kind of example specific - what if I would want to simply get value of something like wmic cpu get maxClockSpeed ?Thanks for your interest anyway. – Paweł Audionysos – 2017-03-24T21:50:55.500

@PawełAudionysos But, is path variable limited only to system drive? No it is not. As far as other commands, IDK (you can try anticipating the result and putting that in your find command. In general though there's probably a better way to do it, I don't know offhand how to do it. – Ploni – 2017-03-24T21:54:28.743

maybe some easy trick to get rid of first line? – Paweł Audionysos – 2017-03-24T21:56:31.800

I'm interested in raw value but this also looks like nice trick :) – Paweł Audionysos – 2017-03-24T22:11:18.927

@PawełAudionysos "easy trick to get rid of first line?" See my answer :) – DavidPostill – 2017-03-25T00:06:10.050

2

Pipe your output into findstr as Ploni suggested, but use the /v option for findstr. That option tells findstr to display only lines that do not contain a match, so you can specify that you don't want to see lines containing "VariableValue". E.g.:

C:\Users\Jane>wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue
VariableValue
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;


C:\Users\Jane>wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue | findstr /v VariableValue
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;


C:\Users\Jane>

Note: you could also specify that you only wanted to ignore lines that begin with VariableValue, if you needed to include lines where it appeared later in the line by using the /R option to findstr, which specifies that you will be using a regular expression and then put a ^ before the search string, since the caret character represents the beginning of a line. E.g., wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue | findstr /V /R "^VariableValue"

Update: As an alternative to the find and findstr commands, a version of the GNU grep utility, which supports regular expressions and is widely used on Linux/Unix systems, is available for Windows. Grep, as well as other GNU utilities for Windows systems can be downloaded from GnuWin Packages.

moonpoint

Posted 2017-03-24T21:16:47.843

Reputation: 4 432

Thanks, it look good but you cannot guarantee that value of property wont have lines that begin with the same sub string as property name. It looks better but it is still not an ultimate solution. Is there any other util available in cmd that provides regular expression? This one is kind of limited, if only it had optional multi-line flag it would be solved already... :/ Do you know if there are any option in wmic so it would not return that line in first place? If there is no such option I'll simply accept the question and ask another question about line removal. – Paweł Audionysos – 2017-03-24T23:26:35.380

@PawełAudionysos "Do you know if there are any option in wmic so it would not return that line in first place?" See my answer :) – DavidPostill – 2017-03-25T00:06:56.937

1

Maybe too late but I think there's a slightly more elegant solution.

wmic allows you to use stylesheets to format the output.

Consider this example:

wmic os get OSArchitecture /format:csv

The output is

Node,OSArchitecture
MY-COMPUTER,64bit

With the argument /format:csv you are telling wmic to use the csv.xls stylesheet located by default in %WINDIR%\wbem\en-US (replace en-Us with your locale).

And now the little magic: you can create your own xsl, tell wmic to use it and format the output as you want.

For example, create a stylesheet single-value-only.xsl

<?xml version="1.0"?>
<!-- Maybe you should refine this stylesheet a bit for a broader or production use but this basically works-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="utf-16" omit-xml-declaration="yes"/>
<xsl:param name="norefcomma"/>
<xsl:template match="/">
<xsl:value-of select="COMMAND/RESULTS[1]/CIM/INSTANCE[1]/PROPERTY/VALUE"/>
</xsl:template> 
</xsl:stylesheet>

And the run wmic

wmic os get OSArchitecture /format:"C:\path\of\single-value-only.xsl"

The result is

64bit

If you are in a batch script and want to put the value into a variable MY_VAR

for /f %%i "delims=" in (`wmic os get OSArchitecture /format:"C:\path\of\single-value-only.xsl"`) do set MY_VAR=%%i

Federico Destefanis

Posted 2017-03-24T21:16:47.843

Reputation: 111

0

Here's another solution:

    for /f "usebackq skip=1 delims== tokens=2" %i in (`wmic environment where ^(name^="PATH" and systemVariable^=FALSE^) get variableValue /FORMAT:TextValueList`) do @echo %i

Note this method only works properly if the values of the wmic query do not contain equal signs.

It essentially works by splitting the list at the equals signs and returning the second result.

Jenna Sloan

Posted 2017-03-24T21:16:47.843

Reputation: 51

Given that this method has a drawback (which you identified) and is longer than David’s, why would anybody want to use this one? … … … … … … … … … … … … … Please do not respond in comments; [edit] your answer to make it clearer and more complete. – Scott – 2019-02-15T05:58:27.673

@Scott Honestly, I think it's easier to understand. – Jenna Sloan – 2019-02-15T12:49:18.223

Hmm.  People might find it easier to understand if you explained it.  For example, I understand what delims== means, but I believe that it’s very cryptic and non-intuitive. – Scott – 2019-02-15T18:09:30.560