#####
#
# This will automatically format all Removable devices connected to the local computer.
#
#####
#
# Use the following command to identify the types of drivives connected to the computer.
# Get-WmiObject win32_LogicalDisk | select DriveType,DeviceID | ForEach-Object { Write-Host $_.DriveType $_.DeviceID -ForegroundColor Cyan }
# 
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx
# Value             Meaning
# 0                 Unknown
# 1                 No Root Directory
# 2                 Removable Disk
# 3                 Local Disk
# 4                 Network Drive
# 5                 Compact Disc
# 6                 RAM Disk
#
#####

Get-WmiObject win32_LogicalDisk | select DeviceID,Size,Description,DriveType,FileSystem,VolumeName |  where { $_.DriveType -like '2' }


Get-WmiObject win32_LogicalDisk | where { $_.DriveType -like '2' } | ForEach-Object { C:\Windows\System32\format.com $_.DeviceID /V:<VolumeName> /Q /FS:FAT32 /y }

# Inform the user that the formatting is complete
[System.Media.SystemSounds]::Beep.Play();
Start-Sleep -Seconds 1
[System.Media.SystemSounds]::Beep.Play();
Start-Sleep -Seconds 1
[System.Media.SystemSounds]::Beep.Play();

# Report the success / failure status of the formatted drives
Get-WmiObject win32_LogicalDisk | select DeviceID,Size,Description,DriveType,FileSystem,VolumeName |  where { $_.DriveType -like '2' } | where { $_.FileSystem -like 'FAT32' } | ForEach-Object { Write-Host $_.DeviceID $_.VolumeName $_.FileSystem $_.Description $_.Size -ForegroundColor green }
Get-WmiObject win32_LogicalDisk | select DeviceID,Size,Description,DriveType,FileSystem,VolumeName |  where { $_.DriveType -like '2' } | where { $_.FileSystem -notlike 'FAT32' } |ForEach-Object { Write-Host $_.DeviceID $_.VolumeName $_.FileSystem $_.Description $_.Size -ForegroundColor Red }

Write-Host '##############################'
Write-Host '#                            #'
Write-Host '#    Formatting Complete     #'
Write-Host '#                            #'
Write-Host '##############################'

$LogFolder = "C:\PS_TMP\USBCopyLog"
$Folder2Copy = "C:\PS_TMP\USBDataFiles\*"
$OrigLog = ($LogFolder + "_Orig.txt")
$SDLog = ($LogFolder + "SD1.txt")

# Copy the contents of a directory and it's subdirectories to the SD card
Get-WmiObject win32_LogicalDisk | where { $_.DriveType -like '2' } | Select-Object DeviceID | ForEach-Object { Copy-Item -Path $Folder2Copy -Destination $_.DeviceID -Recurse }

if ( -not (Test-Path $OrigLog) ) { New-Item $OrigLog -ItemType file }
if ( -not (Test-Path $SDLog) ) { New-Item $SDLog -ItemType file }

Get-ChildItem  $Folder2Copy -Recurse | Select-Object Name | Sort-Object -property Name | Export-Csv $OrigLog -NoTypeInformation
Get-WmiObject win32_LogicalDisk | select DeviceID|  where { $_.DriveType -like '2' } | Get-ChildItem  $_.DeviceID -Recurse | Select-Object Name | Sort-Object -property Name | Export-Csv $SDLog -NoTypeInformation

Write-Host '###########################'
Write-Host '#                         #'
Write-Host '#    Copying Complete     #'
Write-Host '#                         #'
Write-Host '###########################'

[System.GC]::Collect()