7

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
  • 229
  • 2
  • 4
  • 8

7 Answers7

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
  • 153,128
  • 77
  • 353
  • 394
  • Thanks. Does having line break in `PATH` leads to any issue or is this just cosmetic approach? – Paweł Audionysos Mar 25 '17 at 00:58
  • @PawełAudionysos I'm not sure what you mean. What are you going to do with the variable? – DavidPostill Mar 25 '17 at 08:57
  • 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 Mar 25 '17 at 17:08
  • @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?](//superuser.com/q/284342) – DavidPostill Mar 25 '17 at 17:10
  • 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 `path`variable, but there is no mention how to solve that issue (for example the method in my question)... – Paweł Audionysos Mar 25 '17 at 17:29
  • @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?](//superuser.com/a/1070354) – DavidPostill Mar 25 '17 at 17:36
  • 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 Mar 25 '17 at 18:22
6

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

j-hap
  • 61
  • 1
  • 2
5

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
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.

  • How does that suppose to work? – Paweł Audionysos Mar 24 '17 at 21:36
  • @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 –  Mar 24 '17 at 21:40
  • 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 Mar 24 '17 at 21:50
  • @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. –  Mar 24 '17 at 21:54
  • maybe some easy trick to get rid of first line? – Paweł Audionysos Mar 24 '17 at 21:56
  • I'm interested in raw value but this also looks like nice trick :) – Paweł Audionysos Mar 24 '17 at 22:11
  • @PawełAudionysos "easy trick to get rid of first line?" See my answer :) – DavidPostill Mar 25 '17 at 00:06
1

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
  • 5,080
  • 2
  • 19
  • 22
  • 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 Mar 24 '17 at 23:26
  • @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 Mar 25 '17 at 00:06
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.

  • 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 - Слава Україні Feb 15 '19 at 05:58
  • @Scott Honestly, I think it's easier to understand. – Jenna Sloan Feb 15 '19 at 12:49
  • 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 - Слава Україні Feb 15 '19 at 18:09
0

Or do the equivalent in powershell:

get-ciminstance win32_environment | 
  where {$_.name -eq 'path' -and $_.systemVariable -eq $false} | % variableValue
js2010
  • 575
  • 5
  • 6