You do not have to remove all spaces, as some answers suggest, as some computers have spaces in the middle of the value. See the WMIC CSProduct Test Results List .
Not only do I get a few trailing spaces, but I also get a carriage return ( <CR> ), which is an artifact of automatically converting WMIC unicode output to ASCII.
The final <CR> can be deleted by passing the value through another FOR / F loop. This also removes the phantom "empty" string (actually a <CR> ), so the IF statement is no longer needed.
Trailing spaces can be removed using the ~nx modifier. This works because file names cannot end with a space or a period and are automatically deleted by the OS. Modifiers normalize the "file name" by removing endpoints and spaces. This will not work properly if the value contains \ , / , * or ? , or if it begins with a letter followed by :
for /F "skip=1 delims=" %%A in ( 'wmic csproduct get name' ) do for /f "delims=" %%B in ("%%A") do set "model=%%~nxB"
Another option is to use the CSV format wmz sentence, although I still have to remove the final <CR> .
for /F "skip=2 tokens=2 delims=," %%A in ( 'wmic csproduct get name /format:csv' ) do for /f "delims=" %%B in ("%%A") do set "model=%%B"
EDIT - Why wmz solution can work without deleting <CR>
The <CR> problem may not be obvious, depending on how you access the model variable after setting it. A normal extension will strip any <CR> , but a slower extension will retain it.
@echo off setlocal enableDelayedExpansion for /f "skip=2 tokens=2 delims=," %%A in ( 'wmic csproduct get name /format:csv' ) do set "model=%%A" echo Normal expansion strips ^<CR^>: [%model%] echo Delayed expansion preserves ^<CR^>: [!model!]
--- OUTPUT ---
Normal expansion strips <CR>: [LX4710-01] ] Delayed expansion preserves <CR>: [LX4710-01
EDIT is another way to avoid the <CR> problem
You can use the CSV format and request an additional attribute that appears after the desired value. <CR> will be discarded along with an additional value when analyzing the desired token.
for /f "skip=2 tokens=2 delims=," %%A in ( 'wmic csproduct get name^,vendor /format:csv' ) do set "model=%%A"