I got a question on a community about how to copy files to all USB
devices plugged in to a computer.
So I started to play around with the WMI class Win32_LogicalDisk
$myDisks = Get-WmiObject -Class Win32_LogicalDisk foreach ($objDisk in $myDisks) { $objDisk | Select DeviceID, DriveType }
This will only show the Drive Type in numbers and this is not so common knowledge
So I went to http://msdn.microsoft.com and did a search on Win32_LogicalDisk.
The first result gave me http://msdn.microsoft.com/en-us/library/aa394173%28VS.85%29.aspx
And there I found this:
So in my case I can now see that my DeviceID E: should be my USB device and that is right.
To do a script that will tell us information that we understand we just add a switch.
$myDisks = Get-WmiObject -Class Win32_LogicalDisk foreach ($objDisk in $myDisks) { $myDreviceID = $objDisk.DeviceID $myDriveType = $objDisk.DriveType switch ($myDriveType) { 0 {$myDriveType = "Unknown"} 1 {$myDriveType = "No Root Directory"} 2 {$myDriveType = "Removable Disk"} 3 {$myDriveType = "Local Disk"} 4 {$myDriveType = "Network Drive"} 5 {$myDriveType = "Compact Disc"} 6 {$myDriveType = "RAM Disk"} } $myDreviceID + " " + $myDriveType }
Now it’s time for the copy part.
I love Robocopy, so I use it in my script.
$myDisks = Get-WmiObject -Class Win32_LogicalDisk $mySourceFiles = "c:\files\" foreach ($objDisk in $myDisks) { $myDeviceID = $objDisk.DeviceID $myDriveType = $objDisk.DriveType switch ($myDriveType) { 0 {$myDriveType = "Unknown"} 1 {$myDriveType = "No Root Directory"} 2 {$myDriveType = "Removable Disk"} 3 {$myDriveType = "Local Disk"} 4 {$myDriveType = "Network Drive"} 5 {$myDriveType = "Compact Disc"} 6 {$myDriveType = "RAM Disk"} } if ($myDriveType -eq "Removable Disk") { robocopy $mySourceFiles $myDeviceID /e } }
No related posts.
Blog post about view and copy to USB disks, http://tinyurl.com/nzhmvu