Find a computer’s model using PowerShell

Interestingly this blog’s most popular post is one where I demonstrate how to find the serial number of a PC in a batch file so you can write a script that does different things for different computer models.

I pretty much don’t write batch code any more, instead using PowerShell as much as possible. So I thought I’d add a post explaining how to get your computer model in PowerShell as well. If you’ve any experience using PowerShell then you know this could be a very short post…

And the answer?

(Get-WmiObject -Class:Win32_ComputerSystem).Model

Extending that to a specific model to allow us to work on something specific we have many options to play with. My preferred method if I’m only dealing with one model would be to filter at the point of making the query, like so:

Get-WmiObject -Class:Win32_ComputerSystem -Filter:"Model LIKE '%H77ITX%'" -ComputerName:localhost

Notice I’ve filtered the query using the LIKE operator which in WMI queries requires the % character as the wildcard indicator, not *. I’ve also specified the ComputerName as localhost in the above commands. Usually WMI commands operate on network objects, even if you only have the one PC, and as a result can take a little time to respond. If you specify localhost it speeds up the command and ensures you only get a result from the PC where you run your script. Of course if you want to do this on remote PCs then you’d use the ComputerName to specify one or more remote PCs.

Anyway that command is only going to return the object if we are running the query on the right PC model. That’s not too useful on its own so we need to make it return something like True or False, then we can work inside an If statement perhaps. Actually we don’t need to bother! If handles a returned object as if that means True, and no returned object as meaning False.

For example:

If(Get-WmiObject -Class:Win32_ComputerSystem -Filter:"Model LIKE '%H78ITX%'" -ComputerName:localhost)
{ Write-Host "Found an H78ITX model" }
Else
{ Write-Host "Model not found" }

OK so what do we do if we’ve got different models and we want to do different things on different models? Easy, go back to the first command, and use its output with the Switch statement.

$pcModel = (Get-WmiObject -Class:Win32_ComputerSystem).Model
Switch -wildcard ($pcModel)
{
    "*Latitude*" { # install special apps for a Dell }
    "*Elite*"    { # install special apps for an HP }
    default      { # install other things for everything else }
}

There’s plenty you can do with the switch statement, even using regex. There’s a good explanation of it here.

Of course you might want to do things for specific models, and also things for all models from a particular vendor. We can achieve that by being less specific with our initial command and just select different properties of the object from the variable like this.

$computerSystem = (Get-WmiObject -Class:Win32_ComputerSystem)
Write-Host $computerSystem.Manufacturer
Write-Host $computerSystem.Model

Hopefully that shows you how to get the model and manufacturer, then you can construct some logic around both.

Finally, something else to consider is that if you want to drill down using the serial number of a machine we need to make use of a different WMI object, namely Win32_BIOS, like this:

(Get-WmiObject -Class:Win32_BIOS).SerialNumber

  • rsanc516

    This is a great article. I am trying to get the models from a list of computer names that I have stored in a text file. There are about 150 computers. How can I do that from powershell?
    Thanks

  • Thanks! What you ask is very easy with PowerShell. First make sure your text file just has all the computers listed without any header or anything, like this:
    computer1
    computer2
    computer3

    Then we have to import the file, and loop through each line piping the output to the gwmi command, like so:
    Get-Content list.txt | %{Get-WmiObject -Class:Win32_ComputerSystem -ComputerName:$_} | Select Name, Manufacturer, Model | Format-Table -AutoSize

    Easy! That command also pipes the result to a select command which allows us to output just the bits we want, followed by the format-table command to output a nice result.
    Hope that helps.

  • DAL

    Hi!! I am very new to scripting (no matter what I use PowerShell or Vbscript, Batch files, etc.) !!! I am trying to write a script to copy files/folder from server to pc’s. I started with this script….

    ‘Network Drive using the Network object
    Set oWshSh = Wscript.CreateObject(“Wscript.Shell”)
    Set FSO =CreateObject(“scripting.FileSystemObject”)

    ‘Copy Folder
    FSO.CopyFolder \servernamefolder1, “C:Testfolder1” ,True

    ‘Release objects… always a good practice.
    Set FSO = nothing

    It works fine when I run it locally!!
    So now I have two separate models that I need to work with… Model OptiPlex 9010 and OptiPlex 9020. I started reading your above script… so I created 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%
    IF /I “%machine%” == “OptiPlex 9010 AIO” goto 9010
    IF /I “%machine%” == “OptiPlex 9020 AIO” goto 9020

    :9010
    xcopy “\servernamefolder1, “C:TestFolder1”
    goto END

    :9020
    xcopy “\servernamefolder2, “C:TestFolder2”
    goto END

    :END
    rem END

    I am very confused and hope you could assist in anyway!! I run the above script and get error message on first line, first char…
    Thanks so much in advance!!

  • Hi DAL. Actually I tried your script and it works fine for me! You’ll need some extra quotes in the lines with xcopy in place of the commas that you’ve used for some reason, and just before the :9010 you’ll need a goto END as well so it just to the end if the model it runs on isn’t one of the two you’ve catered for. Otherwise it works for me exactly as you’ve quoted it here.