Search code examples
windowsbatch-filewmic

How to write a batch file to return a WMIC result


How do I write a batch file to return true if the manufacturer of the motherboard is ASUSTek COMPUTER INC. and return false if it's not using the wmic command?

The command is as below:

wmic baseboard get Manufacturer

and it returns:

Manufacturer
ASUSTeK COMPUTER INC.

and I only need to compare the string ASUSTeK COMPUTER INC..


Solution

  • To capture the output of the wmic command use for /F:

    set "BOARD="
    for /F "skip=1 delims=" %%I in ('
        wmic BaseBoard get Manufacturer
    ') do (
        for /F "delims=" %%J in ("%%I") do (
            set "BOARD=%%J"
        )
    )
    rem // Compare retrieved string:
    if /I "%BOARD%"=="ASUSTeK COMPUTER INC." (
        echo True
    ) else (
        echo False
    )
    

    The two nested for /F loops are necessary to properly convert the Unicode wmic output.


    However, you could also filter the wmic output directly, like this:

    wmic BaseBoard where "Manufacturer='ASUSTeK COMPUTER INC.'" get Manufacturer 2>&1 > nul | find /V "" > nul && (echo False) || (echo True)
    

    The where clause does the filtering; if no match is found, No Instance(s) Available. is returned on the STD_ERR stream (handle 2). The expression 2>&1 > nul suppresses the STD_OUT stream (handle 1) and redirects STD_ERR to STD_OUT instead, so find is going to receive it; the search expression /V "" finds a match when the stream is not empty. find returns an exit code of 0 if a match is found and 1 otherwise; the operators && and || check the exit code and execute the respective echo command contitionally.