Search code examples
wmic

WMIC output in a batch script


This is pretty straight forward but after much Googling and experimenting, I cannot find the answer. I will use this as an example and then I can apply it to other scripts I am writing. When I run this command

wmic cpu get name > C:\Temp\PCINFO_TEMp\core.txt

I get this:

Name
Intel(R) Core(TM) i5-10310U CPU @ 1.70GHz

Is there a way to get the Intel info without getting Name? I have tried Findstr and find and it wouldnt work.

Any help is appreciated


Solution

  • The Unicode output of WMIC encoded with UTF-16 LE with BOM (byte order mark) can be filtered with two FOR loops to get just the wanted data written into an ASCII encoded text file.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    for /F "delims=" %%I in ('%SystemRoot%\System32\wbem\wmic.exe CPU GET Name /VALUE') do for /F "tokens=2 delims==" %%J in ("%%I") do >"C:\Temp\PCINFO_TEMP\core.txt" echo %%~J
    endlocal
    

    The usage of just one FOR loop as follows does not really work.

    for /F "tokens=2 delims==" %%I in ('%SystemRoot%\System32\wbem\wmic.exe CPU GET Name /VALUE') do >"C:\Temp\PCINFO_TEMP\core.txt" echo %%I
    

    The reason is that the Windows command processor has a problem on processing the UTF-16 encoded output of WMIC and would write the data with two carriage returns and a line-feed into the text file instead of just carriage return + line-feed. This wrong line termination issue is avoided by using two FOR /F loops.

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    • echo /?
    • endlocal /?
    • for /?
    • setlocal /?
    • wmic /?
    • wmic cpu /?
    • wmic cpu get /?