Powershell – Get Drive Letters by Volume Name and Execute RoboCopy

This script starts a transcript, retrieves a source and destination drive letter using the drive’s “volume name” and proceeds with a RoboCopy session. If one of the volumes is not found, the script exits.

# Begin Transcript 
Start-Transcript -Path "$env:USERPROFILE\Desktop\Primary_to_Backup_Transcript.txt"
# Set variables
$sourceLabel = "Seagate 4TB Primary"
$destinationLabel = "Seagate 4TB Backup"
Write-Host ""

# Get Source Drive
$sourceLetter = Get-WmiObject -Class Win32_Volume | where {$_.Label -eq $sourceLabel} | select -expand name
Write-Host -NoNewline "Source: "
if ($sourceLetter) {
    Write-Host -NoNewline "found " -ForegroundColor Green
    Write-Host -NoNewline "`"$sourceLabel`"" -ForegroundColor Cyan
    Write-Host -NoNewline " at "
    Write-Host "$sourceLetter" -ForegroundColor Cyan
} else { 
    Write-Host "NOT FOUND. :(" -ForegroundColor Red 
}

Write-Host ""

# Get Destination Drive
$destinationLetter = Get-WmiObject -Class Win32_Volume | where {$_.Label -eq $destinationLabel} | select -expand name
Write-Host -NoNewline "Destination: "
if ($destinationLetter) {
Write-Host -NoNewline "found " -ForegroundColor Green
Write-Host -NoNewline "`"$destinationLabel`"" -ForegroundColor Cyan
Write-Host -NoNewline " at "
Write-Host "$destinationLetter" -ForegroundColor Cyan
} else { 
    Write-Host "NOT FOUND. :(" -ForegroundColor Red 
}

# continue copy or exit
if ($sourceLetter -and $destinationLetter) {
    Write-Host ""
    Write-Host "Continuing with copy..." -ForegroundColor Green
    Write-Host ""
    pause
    Robocopy.exe $sourceLetter $destinationLetter /MIR /R:0 /W:1 /V 
} else { 
    Write-Host ""
    Write-Host "Exiting..." -ForegroundColor Red
    Write-Host ""
    pause
    exit
}

Leave a Reply

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