Apr 172008
 

Say you’ve got a script and you want to run something in that script that only runs if you’re on a specific type of machine, what can we do to find out what machine we are on?
Some might say “use vbscript and do a wmi call”. Well yes you could do that, but that’s needlessly hard! Use this simple command instead…

wmic csproduct get name

On my machine that returns two lines, one saying Name and another with my machine’s model name: HP Compaq dc7600 Small Form Factor
How cool is that!! Basically here we’re using a WMI command line tool. There’s lots to it, just type: wmic /?

So to use this in a script we’ll need to check for the existence of a particular machine name being returned, we can use the find command a different way and return the number of lines with a specific word in it. If we get 1 or more lines with that word then we’re on the machine we’re looking for.
Here’s an answer:

wmic csproduct get name | find /c /i "7600"

On my machine that returns a 1, so we just need to parse that into a variable and use a simple if statement and we’re there:

@echo off
for /f "delims==" %%a in ('wmic csproduct get name ^| find /c /i "7600"') do set /a machine=%%a
if %machine% geq 1 (
echo Running on a 7600 machine
) else (
echo Not running on a 7600 machine
)

[script now works, thanks to the comment pointing to the pipe-char issue]

You could modify that to do a goto to jump to another part of your script I guess. Just change the "7600" part for something that uniquely identifies the machine you’re looking for in your environment.

Ths works on XP and Vista. I think it should work in WinPE too as long as you have the WMI add-in installed.

– Updated 2011-10-06 –

And now here’s a better version of my script which will help people who want to use it to batch things a bit more easily!

@ECHO OFF
REM do a wmi query to get the info we want and put it in a variable
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Name /VALUE ^| FIND /I "Name="') DO SET machine=%%A
ECHO Computer model: "%machine%"

REM Now we have the model in a variable we can do some logic and run commands, for example...
REM Watch for stray spaces at the end, take out all spaces with: SET machine=%machine: =%
IF /I "%machine%" == "Latitude E6410" (
REM do something specific for an E6410
) ELSE (
REM do something for other types
)

– Updated 2013-09-21: new version of this article using PowerShell here

  • Anonymous

    Worked very well! Just curious though, I remoted into a server and ran this command “wmi csproduct get name” and it returned the model of MB. Anyway to get around that?

  • NiXC

    Interesting… if you try just “wmic csproduct” you’ll get a bit more information, eg. on a server here I get the Dell product number, the vendor, the name (PowerEdge 2650) and a few other things. Perhaps your box’s information is stored in a different place. If it’s in there you could change the command to bring back the different field, e.g. “wmic csproduct get IdentifyingNumber”
    Could it be the server is a custom built one?

  • NiXC

    Ah ha! Well found Anonymous! Testing my script I see that blogger’s post editor has replaced the ‘pipe’ symbol in the for statement and preceding the ‘find’ statement with a wrong character. Whilst I try to fix it the answer is the replace the character with the one that’s above your “” character on your keyboard, the normal pipe character.
    Thanks for the comment.

  • Anonymous

    Great find! However, I’m getting an error stating “Invalid GET Expression”. What could be wrong?

  • anonymous

    Excellent post. Works well.
    I could not figure out how to install drivers from the command line based on a PC model. This did the trick.

  • Landon

    I copied the script exactly as you have it but it will not run. I even placed pauses to locate where it errors out at and it bombs out on the

    wmic csproduct get name ^| find /c /i “7600?

    portion of the script. Any ideas? I am running this as an administrator on a Windows Vista machine.

  • NiXC

    Hi Landon, that’s a problem with the copy/paste. Try replacing all the quotes and apostrophes with plain-text quotes and apostrophes and it’ll work hopefully. I’ll try and fix the post. This happens a lot with posting code on wordpress blogs, sorry.

  • SYAM KUMAR

    I Have one old mother board. i found only TAS 630 VER:1.0,
    630E PRO-M9209002,
    S/N:20031 02402007,
    T584010A, only marks
    How to found my mother board make & model no?
    i think that 2002 make

  • NiXC

    Sounds like a PC Chips TAS630 to me.

  • Xeon

    Hi,

    if I run the script I get “ö was unexpected at this time.” any ideas?

    here is my edited one:

    @echo off
    for /f “delims==” %%a in (‘wmic csproduct get version ^| find /c /i “T520″‘) do set /a machine=%%a
    if %machine% geq 1 (
    echo Running on a 7600 machine
    ) else (
    echo Not running on a 7600 machine
    )

  • NiXC

    Try a find/replace using notepad on your script and replace all the ” symbols with ” again, and ‘ symbols with ‘ and just in case replace all the spaces with a space as well. Sometimes copying and pasting from a webpage gives you odd characters that the cmd-line can’t use, and it sounds like that’s happened to you this time!

  • Xeon

    Hi,

    I have retyped the script to a new file, then I get: “: was unexpected at this time.” but I’m not using : i’m using |

  • NiXC

    Hi Xeon, sounds like you’re using the wrong “pipe” character. Try the other one! And don’t forget the ^ character, that’s important in the script.

  • Xeon

    I got it to work, but was wondering how can I save only the value output?

    i.e. if i run wmic csproduct get version /value i get the full computer model number but I want to save the results after the “=” to a variable

    here is the output for command window:
    C:\Windows\system32>wmic csproduct get version /value

    Version=ThinkPad T520

  • NiXC

    I think that’s the problem I had to deal with too, and never did properly, but that was why I did the above script. Without the /value part your output will have two lines, and my script essentially takes the second line of the output and puts it in the variable. Perhaps you can modify it to put both lines in different variables, and just ignore the first variable altogether.

    This of course is the whole problem with command line scripting, parse and handling values is needlessly difficult. PowerShell of course is the solution here…

  • Xeon

    Hi NiXC

    I got it but only for lenovo… ;-)

    for /f “delims==” %%a in (‘wmic csproduct get version ^| find /n “Think” ‘) do set model=%%a
    set model=%model:~12,10%
    echo %model%
    pause.

    thanks for your help!!

  • Woosel

    I’m trying to get a specific video driver to install based on the model of computer. I’m pretty new at complex batch files, but here’s what I have:

    @echo off
    for /f “delims==” %%a in (‘wmic csproduct get name ^| find /c /i “Latitude E6410″‘) do set /a machine=%%a
    if %machine% geq 1 goto 6410
    ELSE
    for /f “delims==” %%a in (‘wmic csproduct get name ^| find /c /i “Latitude E6420″‘) do set /a machine=%%a
    if %machine% geq 1 goto 6420
    :6410
    C:\Drivers\E6410\Video\setup /whatever
    goto END
    :6420
    C:\Drivers\E6420\Video\setup /whatever
    goto END
    :END
    END

    Probably way off base, can you help point me in the right direction?

  • NiXC

    Hi Woosel, I have added a new bit of code to my post about this which should help you out.

  • Woosel

    It worked!!! Thanks SO much! I found the old “labels and sections” method easier to track and manage visually, here’s what it wound up looking like:

    @ECHO OFF
    FOR /F “tokens=2 delims==” %%A IN (‘WMIC csproduct GET Name /VALUE ^| FIND /I “Name=”‘) DO SET machine=%%A
    ECHO Computer model: %machine%
    IF /I “%machine%” == “Latitude E6400″ goto 6400
    IF /I “%machine%” == “Latitude E6410″ goto 6410
    IF /I “%machine%” == “Latitude E6420″ goto 6420

    :6400
    “C:Driversetcinstall.bat”
    goto END

    :6410
    “C:Driversinstall.bat”
    goto END

    :6420
    “C:DriversInstall.bat”

    :END
    rem END

  • Woosel

    BTW i know the driver paths are identical…they are just listed as examples as in the actual script they are of course unique.

  • Woosel

    Found a problem preventing the script from fully running. While the correct model is being returned, some extra spaces are occurring that prevent conditional actions. For example, running the following on a Latitude D620, it detects the model correctly but won’t do the ECHO command:

    @ECHO OFF
    FOR /F “tokens=2 delims==” %%A IN (‘WMIC csproduct GET Name /VALUE ^| FIND /I “Name=”‘) DO SET machine=%%A
    ECHO Computer model: %machine%
    ECHO Installing Wireless Utilities if applicable, please wait
    IF /I “%machine%” == “Latitude D620 ” ECHO A
    PAUSE

    The output is as follows:

    Computer model: Latitude D620
    Installing Wireless Utilities if applicable, please wait
    Press any key to continue . . .

    If we put an ECHO in front of the IF /I line we get this:
    Computer model: Latitude D620
    Installing Wireless Utilities if applicable, please wait
    IF /I “Latitude D620 ” == “Latitude D620 ” ECHO A
    Press any key to continue . . .

    Somehow I am wondering if those extra spaces are preventing the conditional processing. The entire file I am trying to run is this:

    @ECHO OFF
    FOR /F “tokens=2 delims==” %%A IN (‘WMIC csproduct GET Name /VALUE ^| FIND /I “Name=”‘) DO SET machine=%%A
    ECHO Computer model: %machine%
    ECHO Installing Wireless Utilities if applicable, please wait
    IF /I “%machine%” == “Latitude D620″ goto LAT_WIRELESS
    IF /I “%machine%” == “Latitude D630″ goto LAT_WIRELESS
    IF /I “%machine%” == “Latitude D430″ goto LAT_WIRELESS
    IF /I “%machine%” == “Optiplex GX620″ goto END
    IF /I “%machine%” == “Optiplex 320″ goto END
    IF /I “%machine%” == “Optiplex 330″ goto END
    IF /I “%machine%” == “Optiplex 745″ goto END
    IF /I “%machine%” == “Optiplex 755″ goto END
    IF /I “%machine%” == “Optiplex 760″ goto END
    IF /I “%machine%” == “Optiplex 780″ goto END
    IF /I “%machine%” == “Optiplex 790″ goto END
    IF /I “%machine%” == “Lenovo X61″ goto END
    GOTO END

    :LAT_WIRELESS
    “C:WindowsScriptsNcinst.exe” /s
    “C:DriversHMHITD620 – D630 – D430 WLAN UtilityDell WLANsetup.exe” /s /v”FRESHINSTALL=True”

    :END
    ECHO Utility Install Complete…exiting in 5 seconds
    ping 127.0.0.1 -n 6 >nul
    EXIT

  • Woosel

    Sorry, I missed one part…when you add an ECHO to the front of the IF /I and run it, you get this:

    Computer model: Latitude D620
    Installing Wireless Utilities if applicable, please wait
    IF /I “Latitude D620 ” == “Latitude D620 ” ECHO A
    Press any key to continue . . .

    See the extra spaces returned?

  • Woosel

    WOOHOO! Got it working with help from justanswer.com (was in a hurry). The problem is that while the ECHO computer model will return plain English “Latitude D620″, if you put an ECHO in front of the first IF /I statement, it will return a bunch of extra spaces:

    Computer model: Latitude D620
    Installing Wireless Utilities if applicable, please wait
    IF /I “Latitude D620 ” == “Latitude D620″ ECHO A
    Press any key to continue . . .

    See the extra spaces? The solution to make the information actionable is to add logic to take out all spaces and then adjust your IF conditions accordingly:

    @ECHO OFF
    FOR /F “tokens=2 delims==” %%A IN (‘WMIC csproduct GET Name /VALUE ^| FIND /I “Name=”‘) DO SET machine=%%A
    ECHO Computer model: %machine%
    SET machine2=%machine: =%
    ECHO Installing Wireless Utilities if applicable, please wait
    ECHO IF /I “%machine2%” == “LatitudeD620″ ECHO A

    Result will be:
    Computer model: Latitude D620
    Installing Wireless Utilities if applicable, please wait
    IF /I “LatitudeD620″ == “LatitudeD620″ ECHO A
    Press any key to continue . . .

    See how the two machine values now match exactly without the extra spaces in the first one?
    Now it will return this without the leading ECHO:

    Computer model: Latitude D620
    Installing Wireless Utilities if applicable, please wait
    A
    Press any key to continue . . .

    So, an example of a complete batch file would look like this (note the spaces taken out of the model name)

    @ECHO OFF
    FOR /F “tokens=2 delims==” %%A IN (‘WMIC csproduct GET Name /VALUE ^| FIND /I “Name=”‘) DO SET machine=%%A
    ECHO Computer model: %machine%
    SET machine2=%machine: =%
    ECHO Installing Wireless Utilities if applicable, please wait
    IF /I “%machine2%” == “LatitudeD620″ goto LAT_WIRELESS
    IF /I “%machine2%” == “LatitudeD630″ goto LAT_WIRELESS
    IF /I “%machine2%” == “LatitudeD430″ goto LAT_WIRELESS
    IF /I “%machine2%” == “OptiplexGX620″ goto END
    IF /I “%machine2%” == “Optiplex320″ goto END
    IF /I “%machine2%” == “Optiplex330″ goto END
    IF /I “%machine2%” == “Optiplex745″ goto END
    IF /I “%machine2%” == “Optiplex755″ goto END
    IF /I “%machine2%” == “Optiplex760″ goto END
    IF /I “%machine2%” == “Optiplex780″ goto END
    IF /I “%machine2%” == “Optiplex790″ goto END
    IF /I “%machine2%” == “LenovoX61″ goto END
    GOTO END

    :LAT_WIRELESS
    “C:WindowsScriptsNcinst.exe” /s
    “C:DriversHMHITD620 – D630 – D430 WLAN UtilityDell WLANsetup.exe” /s /v”FRESHINSTALL=True”

    :END
    ECHO Utility Install Complete…exiting in 5 seconds
    ping 127.0.0.1 -n 6 >nul
    EXIT

  • http://myserverissick.com Nick Clark

    Thanks for the detailed replies there Woosel!
    Another way you can get rid of your trailing spaces is to use this little function (which I found on dostips.com):
    for /l %%a in (1,1,31) do if “!machine:~-1!”==” ” set machine=!machine:~0,-1!

    That way you only get rid of the spaces at the end and not all the space from the whole string, which some might prefer. I imagine this might have become a problem because of the way the manufacturers populate the bios entries, or something like that.
    Anyway, nice one. I’ll see if I can incorporate that into the script. Cheers.

  • Timelord83

    So I am at an Impasse. I am using your script and it pulls the model with ease and when i echo it out in “”‘s there seem to be no extra spaces but when i try to use it like this
    FOR /F “tokens=2 delims==” %%A IN (‘WMIC csproduct GET Name /VALUE ^| FIND /I “Name=”‘) DO SET model=%%A

    ECHO Computer Model:”%model%”

    if %model% == “Aspire 7750Z” goto 7750

    I get “7750Z was unexpected at this time.” as an error.. Now I have the :7750 setup and right its just echoing out TADA until i get this working any ideas? Yes it is outputting Aspire 7750Z with a space as the model number.

  • http://myserverissick.com Nick Clark

    Hi Timelord, I’ve had a look and you’ve missed out some double-quotes around the %model% in your IF statement. You need that in case your model has a space in the text.

    So the last line you’re quoted from you’re script would looks like this:
    if “%model%” == “Aspire 7750Z” goto 7750

    Hope that helps.

  • Christopher Holmes

    SET INPUT=0
    SET Manufacturer=0
    SET Model=0

    SET INPUT=Manufacturer
    for /f “tokens=1,* delims==” %%i IN (‘wmic computersystem get %INPUT% /VALUE ^| FIND /I “%INPUT%=”‘) do set Manufacturer=%%j

    SET INPUT=Model
    for /f “tokens=1,* delims==” %%i IN (‘wmic computersystem get %INPUT% /VALUE ^| FIND /I “%INPUT%=”‘) do set Model=%%j

    Echo The Manufacturer is %Manufacturer% and the Model is %Model%

    IF “%Manufacturer%” EQU “Hewlett-Packard” GOTO :HP
    IF “%Manufacturer%” EQU “VMWare, Inc.” GOTO :Virtual
    IF “%Manufacturer%” EQU “Microsoft Corporation” GOTO :Virtual
    IF “%Manufacturer%” EQU “Dell Inc.” GOTO :Dell
    GOTO :EOF

    :Dell
    Echo Dell Found
    REM Example
    IF “%Model%” EQU “PowerEdge R900″ echo 1
    GOTO :EOF

    :HP
    Echo HP Found
    IF “%Model%” EQU “HP Z200 Workstation” echo 1
    GOTO :EOF

    :Virtual
    Echo Virtual Found
    GOTO :EOF