The wmic
command produces Unicode output which for /F
has problems with, so unwanted artefacts remain. In fact those are orphaned carriage-return characters. The best and most flexible method to overcome this is to nest another for /F
loop so that the wmic
output is parsed twice.
The following command line is typed directly into the console, the respective output is also shown:
U:\>for /F "delims=" %I in ('wmic ComputerSystem GET Model /VALUE') do @for /F "delims=" %L in ("%I") do set "output=%L"
U:\>set "output=Model=HP ProDesk 600 G1 SFF"
U:\>
To use the above command line in a batch file you need to double the percent signs:
@echo off
for /F "delims=" %%I in ('wmic ComputerSystem GET Model /VALUE') do (
for /F "delims=" %%L in ("%%I") do set "output=%%L"
)
echo %output%
To strip off the Model=
prefix from the value of the variable output
, change the script to this:
@echo off
for /F "tokens=1,* delims==" %%I in ('wmic ComputerSystem GET Model /VALUE') do (
for /F "delims=" %%L in ("%%J") do set "output=%%L"
)
echo %output%
The double-parsing method is actually credited to dbenham -- see his answer to Why is the FOR /f loop in this batch script evaluating a blank line? and also his external article WMIC and FOR /F : A fix for the trailing <CR> problem.