WMIC output property value without property name
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 thewmic
output.skip=1
skips the header line (containingVariableValue
)findstr /r /v "^$"
removes the trailing blank line from thewmic
output.
Example output:
C:\Users\DavidPostill\AppData\Roaming\npm
Further Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- findstr - Search for strings in files.
- for /f - Loop command against the results of another command.
- wmic - Windows Management Instrumentation Command.
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
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.