Backup Windows user's profile I do a lot of PC swaps/replacements for my role. I've decided to write a script to assist with backing up the user's profile and network drives.

I'd like to see how my script stands now. I'm still working to implement more features such as backing up saved wireless networks and printers. 

#get current user's ID
$userID=$env:UserName

#####################################
### Backup user's saved documents ###
#####################################

$FoldersToCopy = @(
    'Desktop'
    'Downloads'
    'Favorites'
    'Documents'
    'Pictures'
    'Videos'
    'AppData'
    )

$SourceRoot      = "C:\Users\$User"
$DestinationRoot = "$PSScriptRoot\fileBackup\$userID\"

foreach( $Folder in $FoldersToCopy ){
    $Source      = Join-Path -Path $SourceRoot -ChildPath $Folder
    $Destination = Join-Path -Path $DestinationRoot -ChildPath $Folder

    Robocopy.exe $Source $Destination 
    }

#####################################
###  Backup user's mapped drives  ###
#####################################

$mappedDrives = @()
$drives = Get-PSDrive -PSProvider FileSystem

foreach ($drive in $drives) {
    if ($drive.DisplayRoot) {
        $mappedDrives += Select-Object Name,DisplayRoot -InputObject $drive
    }
}

New-Item -ItemType Directory -Force -Path $PSScriptRoot\fileBackup\$userID\mappedDrives
$mappedDrives | Export-Csv $PSScriptRoot\fileBackup\$userID\mappedDrives\mappedDrives.csv
Your code looks pretty good to me. A few notes:



You have a bug here:

$userID=$env:UserName

$SourceRoot      = "C:\Users\$User"


You are referring to $User, which hasn't been set. I always put this at the top of my scripts to catch such bugs:

Set-StrictMode -Version Latest


Why introduce a new variable, by the way? Why not just use it directly?:

$SourceRoot      = "C:\Users\$env:UserName"




This line can be simplified for the sake of readability:

$Source      = Join-Path -Path $SourceRoot -ChildPath $Folder


To this:

$Source      = Join-Path $SourceRoot $Folder




This code would be cleaner using the pipeline:

$mappedDrives = @()

foreach ($drive in $drives) {
    if ($drive.DisplayRoot) {
        $mappedDrives += Select-Object Name,DisplayRoot -InputObject $drive
    }
}


Like this:

$mappedDrives = $drives | ? $drive.DisplayRoot | Select-Object Name, DisplayRoot


(Not tested because I don't have any mapped drives, but I believe it is correct.)

You can format it like this if you like:

$mappedDrives = $drives |
    ? $drive.DisplayRoot | 
    Select-Object Name, DisplayRoot




This comment is misleading:

#####################################
###  Backup user's mapped drives  ###
#####################################


You aren't backing up the drives. You are recording the drive mappings.