Historical Backups of $Profile

Highlighted
Occasional Contributor

Just curious if/how anyone else out there stores backups of their $Profile for POSH/ISE. After losing a few modifications a while back, I created and Backup-Profile function to copy my live $Profile into my OneDrive.

 

Wasn't sure if anyone else had anything more elegant - but here is what I have shoved at the bottom of my $Profile. It provides a historical backup every time I open ISE. I have it set to 100 historical copies - you can obviously tweak it to suit your needs - While 100 copies may be excessive, I have enough things in my profile that I may not use certain things for a long time.

 

Function Backup-Profile {
    #region Varibles
    $BackupPath = 'C:\users\<username>\OneDrive\PowerShell\__Profile\';
    $HistoricalCount = 100
    #endregion Variables

    #Ensure our BackupPath is a folder that exists.
    New-Item $BackupPath `
		-ItemType Directory `
		-Force `
		-ErrorAction SilentlyContinue ;
    $StorageLocation = $BackupPath;
    
    $basename = (Get-Item $profile).baseName

    $files = ($HistoricalCount..1);

    #Remove 100
    [String]$currentCopy = $basename + ".$HistoricalCount";
    Remove-Item $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 "$StorageLocation\$basename.$file.ps1" `
					-Destination "$StorageLocation\$basename.$newitem.ps1"
            }
    }

	# Move the most recent backup to the .1 backup
    Move-Item $StorageLocation\$basename.ps1 $StorageLocation\$basename.1.ps1

	# Finally copy the current profile to the newest backup.
    Copy $profile $StorageLocation
}

# Now Run our actual backup
Backup-Profile

 

0 Replies