Peaceful mountain lake scene featuring a rustic boathouse and stunning reflections.

Automated Multi-Drive Content Migration with PowerShell

Introduction

In today’s digital world, managing and organizing files across multiple drives is a common challenge. Whether you’re upgrading your storage, consolidating data, or simply reorganizing your file structure, manually moving files can be time-consuming and error-prone. This automates the process of migrating content from multiple source folders to a new destination drive while maintaining the original folder structure and providing detailed progress feedback.

The Script

$source_folders = @(
    "C:\Media",
    "C:\Misc", 
    "C:\Projects",
    "C:\Work",
    "D:\Applications",
    "D:\Games",
    "D:\Software",
    "D:\Downloads",
    "D:\Backups",
    "D:\Media",
    "D:\Archives",
    "D:\Library"
)
$destination_drive = "E:"

$totalFiles = 0
foreach ($source_folder in $source_folders) {
    $destination_path = Join-Path -Path $destination_drive -ChildPath $source_folder.Substring(3)
    if (!(Test-Path -Path $destination_path)) {
        New-Item -Path $destination_path -ItemType Directory | Out-Null
        Write-Output "Created directory: $destination_path"
    }

    $files = Get-ChildItem -Path $source_folder -Recurse
    $totalFiles += ($files | Measure-Object).Count

    foreach ($file in $files) {
        $relative_path = $file.FullName.Substring($source_folder.Length)
        $destination_file = Join-Path -Path $destination_path -ChildPath $relative_path
        $destination_folder = [System.IO.Path]::GetDirectoryName($destination_file)
        if (!(Test-Path -Path $destination_folder)) {
            New-Item -Path $destination_folder -ItemType Directory | Out-Null
        }
        try {
            Write-Output "Moving file: $($file.FullName)"
            Move-Item -Path $file.FullName -Destination $destination_file -ErrorAction Stop
            Write-Output "File moved successfully: $destination_file"
        }
        catch {
            $error_message = "Failed to move file: {0}`nError: {1}" -f $file.FullName, $_.Exception.Message
            $error_file = Join-Path -Path $destination_path -ChildPath ("{0}.txt" -f [System.IO.Path]::GetFileNameWithoutExtension($file.Name))
            Set-Content -Path $error_file -Value $error_message
            Write-Warning $error_message
        }
    }
}

$filesMoved = ($source_folders | ForEach-Object { Get-ChildItem -Path $_ -Recurse } | Measure-Object).Count
Write-Progress -Activity "Moving files" -Status "$filesMoved of $totalFiles files moved" -PercentComplete (($filesMoved / $totalFiles) * 100)

How It Works

This is designed to efficiently migrate files from multiple source folders to a new destination drive. Here’s a breakdown of its key features:

  1. Flexible Source Configuration: The script starts by defining an array of source folders from which files need to be moved. These can be customized to match your specific folder structure.
  2. Automatic Directory Creation: Before moving files, the script creates matching directory structures in the destination drive, preserving your original .
  3. Progress Tracking: The script keeps track of the total number of files and provides real-time progress updates during the migration process.
  4. Error Handling: If any files fail to move, the script creates detailed error logs and continues with the remaining files, ensuring the process doesn’t stop due to individual file issues.
  5. Structure Preservation: The original folder hierarchy is maintained in the destination drive, making it easier to locate files after migration.

Conclusion

This PowerShell script is an invaluable tool for system administrators and power users who need to perform large-scale file migrations. By automating the process, it not only saves time but also reduces the risk of errors that could occur during manual file transfers. Whether you’re upgrading storage systems, reorganizing data, or performing , this script provides a reliable solution for your file migration needs.

Remember to always test the script with a small subset of data first and ensure you have proper backups before performing any large-scale file operations.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress Cookie Plugin by Real Cookie Banner