Backup-Profile.ps1
function Backup-Profile { <# .SYNOPSIS Performs a backup of $profile to a specific location .DESCRIPTION Performs a backup of $profile to a specific location. Able to specify location & number of historical copies to keep. .EXAMPLE Backup-Profile -BackupPath 'c:\mybackups\' -HistoricalCount 5 Keeps 5 historical copies, along with the current copy, and backs them all up in c:\mybackups. #> [CmdletBinding()] param ( [Parameter(Mandatory=$true, Position=0)] [System.String] $BackupPath = 'D:\mriston\OneDrive\PowerShell\__Profile\', [Parameter(Mandatory=$false, Position=1)] [System.Int32] $HistoricalCount = 25 ) #region Varibles ; #endregion Variables #Ensure our BackupPath is a folder that exists. New-Item -Path $BackupPath ` -ItemType Directory ` -Force ` -ErrorAction SilentlyContinue ; $StorageLocation = $BackupPath; $basename = (Get-Item -Path $profile).baseName $files = ($HistoricalCount..1); #Remove 100 [String]$currentCopy = $basename + ".$HistoricalCount"; Remove-Item -Path $StorageLocation\$currentCopy.ps1 -Force ; foreach ($file in $files) { if ($file -eq $HistoricalCount) { #Skip the 100 entry } ELSE { #Move the nth entry to the nth+1 file name #EX Backup 99 now becomes backup 100. # Backup 98 now becomes backup 99. # Backup 97 now becomes backup 98. # Store the 'destination' numbering $newitem = $file + 1 # Move the current file to the destination number Move-Item -Path "$StorageLocation\$basename.$file.ps1" ` -Destination "$StorageLocation\$basename.$newitem.ps1" } } # Move the most recent backup to the .1 backup Move-Item -Path $StorageLocation\$basename.ps1 -Destination $StorageLocation\$basename.1.ps1 # Finally copy the current profile to the newest backup. Copy -Path $profile -Destination $StorageLocation } |