Hi all first timer here and having a bit of a problem with a PS script I’m trying to create.

I am looking to run a PS WMI query on what USB storage devices are connected to a list of devices in ComputerList.txt what I want to achieve is to have the Device name , Device ID, Manufacturer and description then output to DeviceOutput.csv.

$hostname = Get-Content C:\ComputerList.txt
$Output = Invoke-Command -ComputerName $hostname { Get-WmiObject Win32_USBControllerDevice | Select Name,DeviceID,Manufacturer,Description }
$Output | Out-File C:\DeviceOutput.csv

What I have in the output is lacking the relevant information and I am not sure why its failing I think its due to the WMI Win32_USBControllerDevice?

Any help is much appreciated with this one as I’m sure its something simple.

3 Spice ups

you need foreach and export as csv

$hostname = Get-Content C:\ComputerList.txt
$data=ForEach($host in $hostname){
Get-WmiObject Win32_USBControllerDevice -ComputerName $host | 
Select @{n="Computername";e={"$host"}},Name,DeviceID,Manufacturer,Description
}

$data |export-csv  C:\DeviceOutput.csv -NoTypeInformation

Thanks for the prompt reply JitenSH but the script is now having problems as it now errors when running stating problems with the $host variable as it is read-only.

Cannot overwrite variable Host because it is read-only or constant.
At line:2 char:15

  • $data=ForEach($host in $hostname){
  • CategoryInfo : WriteError: (Host:String) , SessionStateUnauthorizedAccessException
  • FullyQualifiedErrorId : VariableNotWritable

Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.
At line:7 char:8

  • $data |export-csv C:\Scripts\DeviceOutput.csv -NoTypeInformation
  • CategoryInfo : InvalidData: (:slight_smile: [Export-Csv], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand

My bad ,Apparently $Host is a reserved word / Variable in Powershell

make it

$hostname = Get-Content C:\ComputerList.txt
$data=ForEach($computer in $hostname){
Get-WmiObject Win32_USBControllerDevice -ComputerName $computer| 
Select @{n="Computername";e={"$computer"}},Name,DeviceID,Manufacturer,Description
}

Thanks for that JitenSH as it has cleared up the error now.

The only problem I have is what I was facing originally and that is that the script runs but all it shows in the “Computer Name” that its taking from the computerlist.txt but is not actually showing any USB storage device information.

This is leading me to think Win32_USBControllerDevice is maybe not the correct WMI item to find out the local USB storage information, no matter how much searching I do I cant find what I need as Win32_PNPEntity covers this but just has to much information to digest and no way to filter it.

There is some references to what I am looking for at How Can I Determine Which USB Devices are Connected to a Computer? - Scripting Blog [archived] but its going over my head as I do not know how best to implement what they are saying to what I need.

If all the target computers are running PS V3, then instead of:

Get-WmiObject Win32_USBControllerDevice -ComputerName $host 

I’d use:

Get-CimInstance -Class Win32_USBControllerDevice -ComputerName $HostToExamine 

That means changing the foreach earlier to look like:

$Data = ForEach ($HostToExamine in $HostName){