View and Copy to USB disks

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
}

image

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:

image

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

}

image

 

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
}

}

Technorati Tags: ,,,

No related posts.

This entry was posted in PowerShell Blogs and tagged , , , . Bookmark the permalink.

0 Responses to View and Copy to USB disks

  1. Fredrik Wall says:

    Blog post about view and copy to USB disks, http://tinyurl.com/nzhmvu

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>