What's In Your Profile - #3 - Backup-Profile

Welcome back everyone! Time for another What's In Your Profile!

As we all know in IT, backups are key! I have this at the bottom of my profile to keep a current backup of my $profile on my OneDrive. However, I was thinking a while back, it sure would be nice to have a few copies of previous iterations. You know, just in case!

The following will allow for 100 (Feel free to change the numbers) backups of my $profile in my OneDrive. Once it reaches 100 (You'll see lots of red in your screen until you hit this point), it deletes the 100th and then copies the current version up.

Until next time! Hope this helps!

Function Backup-Profile {
    New-Item C:\users\mriston\OneDrive\PowerShell\__Profile\ `
  -ItemType Directory `
  -Force `
  -ErrorAction `
  SilentlyContinue ;
    $StorageLocation = "C:\users\mriston\OneDrive\PowerShell\__Profile\";
    $basename = (Get-Item $profile).baseName

    $files = (100..1);

    #Remove 100
    [String]$currentCopy = $basename + ".100";
    Remove-Item $StorageLocation\$currentCopy.ps1 -Force ;

    foreach ($file in $files) {
        if ($file -eq 100) {
   #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


Popular Posts