Search for a string in command line output

Ramesh picture Ramesh · Sep 17, 2012 · Viewed 36.7k times · Source

I want to search for the string "virtual" in "system model" attribute of 'sysinfo' command. The command should be successful if 'virtual' is found in the 'system model: -------------------------' i.e. output of the systeminfo. It should not search for 'virtual' in whole output of systeminfo command but should do in system model attribute only. For example the command

systeminfo | findstr /i "system model" 

I will get something like

System Model:              HP Compaq dc7800p Small Form Factor

in the above line of the output i want to search for string virtual, and want to manipulate using errorlevel. So please help me to do this.

Following is the one I tried which was not correct. Or help me if i can use regular expressions

systeminfo | findstr /i /R  "system model: virtual machine" > nul
if %errorlevel% == 0 (
   echo virtual machine
) ELSE (
   echo physical machine
)

Thanks in advance

Answer

THelper picture THelper · Sep 17, 2012

Try this:

systeminfo | findstr /I /B /C:"system model" | findstr /I "virtual"
if %errorlevel% == 0 (
    echo virtual machine
) else (
    echo real machine
)

I've tested in on a real and virtual system and it works fine on WinXp and Win7. Note that the system model string is only used in English Windows versions. Windows versions in other languages will use a different names.