Hi Guys,<\/p>\n
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.<\/p>\n
Get-CimInstance -classname win32_computersystem\n\n<\/code><\/pre>\n
Advertisement
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?<\/p>\n
Thank you!<\/p>","upvoteCount":11,"answerCount":3,"datePublished":"2022-12-15T00:23:17.000Z","author":{"@type":"Person","name":"ulisescruz","url":"https://community.spiceworks.com/u/ulisescruz"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi Guys,<\/p>\n
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.<\/p>\n
Get-CimInstance -classname win32_computersystem\n\n<\/code><\/pre>\nCould 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?<\/p>\n
Thank you!<\/p>","upvoteCount":11,"datePublished":"2022-12-15T00:23:17.000Z","url":"https://community.spiceworks.com/t/powershell-query-wtih-csv/942412/1","author":{"@type":"Person","name":"ulisescruz","url":"https://community.spiceworks.com/u/ulisescruz"}},{"@type":"Answer","text":"
What have you tried? where are you stuck?<\/p>\n
e.g.<\/p>\n
however you want to add error handling, what if the computer is not reachable and such.<\/p>\n
$computerNameList = get-content \"c:\\folder\\file.txt\"\n\n$modelReport = \nforeach($computer in $computerNameList){\n $cim = $Null\n $cim = (Get-CimInstance -classname win32_computersystem -ComputerName $computer).model\n\n [pscustomojbect]@{\n ComputerName = $computer\n Model = $cim\n }\n}\n\n$modelReport | \nexport-csv \"somefolder\\modelReport.csv\" -NoTypeInformation\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2022-12-15T00:44:53.000Z","url":"https://community.spiceworks.com/t/powershell-query-wtih-csv/942412/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"If your Csv looks like something like this:<\/p>\n
ComputerName\n\"SERVER1\"\n\"SERVER2\"\n<\/code><\/pre>\nThe Csv can have other columns, I just used one for simplicity.<\/p>\n
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.<\/p>\n
$Computers = Import-Csv -Path \"C:\\Path\\File.csv\"\n\nGet-CimInstance -ComputerName $Computers -Class Win32_ComputerSystem\n<\/code><\/pre>\nIf you’re looking for something a little more robust, the Get-DiskUsage<\/a> example of PS2HTMLTable shows how to create an HTML report and log machines with errors.<\/p>\n