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?
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?
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
To omit the header line, simply pipe the output to more and tell it to omit the first line:
wmic environment where(name="PATH" and systemVariable=FALSE) get variableValue | more +1
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/more
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
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.
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.
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.
Or do the equivalent in powershell:
get-ciminstance win32_environment |
where {$_.name -eq 'path' -and $_.systemVariable -eq $false} | % variableValue