Hi Guys,

I have a CSV file with computer names. I would like to execute this powershell command on every PC in order to retrieve the model name of the device.

Get-CimInstance -classname win32_computersystem

Could I get some assistance on how to write a powershell script to bring in the data from CSV and execute the command on every computer name listed on the CSV?

Thank you!

11 Spice ups

What have you tried? where are you stuck?

e.g.

however you want to add error handling, what if the computer is not reachable and such.

$computerNameList = get-content "c:\folder\file.txt"

$modelReport = 
foreach($computer in $computerNameList){
    $cim = $Null
    $cim = (Get-CimInstance -classname win32_computersystem -ComputerName $computer).model

    [pscustomojbect]@{
        ComputerName = $computer
        Model        = $cim
    }
}

$modelReport | 
export-csv "somefolder\modelReport.csv" -NoTypeInformation
3 Spice ups

If your Csv looks like something like this:

ComputerName
"SERVER1"
"SERVER2"

The Csv can have other columns, I just used one for simplicity.

If you pass an array of computers to Get-CimInstance, it will perform them asynchronously which will speed things up if you have a fair amount of devices to check vs. doing one at a time.

$Computers = Import-Csv -Path "C:\Path\File.csv"

Get-CimInstance -ComputerName $Computers -Class Win32_ComputerSystem

If you’re looking for something a little more robust, the Get-DiskUsage example of PS2HTMLTable shows how to create an HTML report and log machines with errors.

1 Spice up