Copy-UserProfileSafe.ps1

<#PSScriptInfo
 
.VERSION 1.0.1
 
.GUID 2d2b504a-1ead-4dbb-b6ce-31f4a75ac61f
 
.AUTHOR Kalichuza
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Copies SAFE User\UserProfile Data to another folder. This will only copy the non-system and non-hidden files. Use this if you think the origin profile has been corrupted for some reason.
 
#>
 

<#
.SYNOPSIS
Transfers specific user profile folders from a local directory to a destination using Robocopy, excluding system and hidden files.
 
.DESCRIPTION
This script copies user profile folders such as Documents, Desktop, Downloads, Favorites, and others from a source path to a destination path using Robocopy. It excludes system and hidden files, making it suitable for use if the origin profile may be corrupted. The script iterates over a list of common user folders, copying each one from the source to the destination, and provides progress and error reporting for each folder. It also supports optional secure credentials using `PSCredential`.
 
.PARAMETER source
The full path to the source directory containing the user profile folders. This is typically a local directory where the data to be transferred is stored. This parameter is required.
 
.PARAMETER destination
The path to the destination directory where the profile will be copied. The destination will be created if it does not exist.
 
.PARAMETER username
The username required for authentication to the destination, typically in the format `domain\username`. This parameter is optional.
 
.PARAMETER password
A secure password associated with the username for authentication to the destination. This parameter is optional.
 
.EXAMPLE
.\Copy-UserProfileSafe.ps1 -source "C:\Users\Profile" -destination "D:\Backup\Profile" -username "domain\admin" -password (ConvertTo-SecureString 'P4ssWord' -AsPlainText -Force)
 
This example copies the safe profile data from `C:\Users\Profile` to `D:\Backup\Profile`, using secure credentials for authentication.
 
.NOTES
- If the source directory does not exist, the script will exit with an error.
- If the destination directory does not exist, the script will create it before copying the files.
- Robocopy is configured to exclude system and hidden files from being copied.
- The script checks for Robocopy exit codes and reports success or errors for each folder copied.
 
.ROBUSTNESS
- The script is designed to handle both local and network file systems, and supports secure credentials for network shares.
 
.HELP
For more information on Robocopy exit codes, refer to:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
#>


param (
    [string]$source,
    [string]$destination,
    [string]$username,
    [System.Security.SecureString]$password
)

# Ensure the source and destination are provided
if (-not $source -or -not $destination) {
    Write-Host "Usage: .\Copy-UserProfileSafe.ps1 -source <source> -destination <destination> [-username <username>] [-password <password>]"
    exit 1
}

# Define the full paths for the source and destination
$sourcePath = $source
$destinationPath = $destination

# Check if the source directory exists
if (-not (Test-Path -Path $sourcePath)) {
    Write-Host "Source path does not exist: $sourcePath"
    exit 1
}

# Create the destination directory if it does not exist
if (-not (Test-Path -Path $destinationPath)) {
    New-Item -ItemType Directory -Path $destinationPath -Force
}

# Use PSCredential if username and password are provided
if ($username -and $password) {
    $credential = New-Object System.Management.Automation.PSCredential($username, $password)
    # Credential object is created for future use (e.g., if network authentication is needed)
}

# Define directories to include
$includeDirs = @("Documents", "Desktop", "Downloads", "Favorites", "Links", "Music", "Pictures", "Videos")

# Build the robocopy command for each directory
foreach ($dir in $includeDirs) {
    $sourceDir = Join-Path -Path $sourcePath -ChildPath $dir
    $destinationDir = Join-Path -Path $destinationPath -ChildPath $dir

    if (Test-Path -Path $sourceDir) {
        $robocopyCommand = "robocopy `"$sourceDir`" `"$destinationDir`" /MIR /COPYALL /R:0 /W:0 /MT:8 /ETA /V /TEE /XJ /A-:SH"
        Invoke-Expression $robocopyCommand

        # Check robocopy exit code
        $robocopyExitCode = $LASTEXITCODE

        if ($robocopyExitCode -le 7) {
            Write-Host "Copied $dir successfully from $sourceDir to $destinationDir."
        } else {
            Write-Host "Error copying $dir. Robocopy exit code: $robocopyExitCode"
        }
    } else {
        Write-Host "Source directory does not exist: $sourceDir"
    }
}

Write-Host "Profile data transfer completed."