On this page you can find things about PowerShell.

Backup and Restore your PowerShell script.

Here is a tutorial how to make a Backup and being able to restore your script from the backup with PowerShell.

So, first things first,
You need a Backup folder for your backup.
If you have the Backup folder ready you can start.

Here is the command how to Backup

#Alias <set alias here>
Function global:<set function here> {
$sourceDir = ‘<path to your script>’;
$targetDir = ‘<path to your backup folder>’
$targetFilenameBase = ‘<What the backup zip name starts with>’;
$Days = 14

## no changes here
$date = get-date;
$dateStr = $date.ToString(“yyyy-MM-dd_HH-mm”);
$targetFile = $targetDir+$targetFilenameBase+$dateStr+’.zip’;

Add-Type -A ‘System.IO.Compression.FileSystem’;[IO.Compression.ZipFile]::CreateFromDirectory($sourceDir, $targetFile);

if (Test-Path $sourceDir) {
    $Now = Get-Date
    $LastWrite = $Now.AddDays(-$Days)
    $Files = Get-ChildItem $sourceDir -recurse | Where {$_.LastWriteTime – le “$LastWrite”}
    if ($files) { 
       [io.compression.zipfile]::CreateFromDirectory($sourceDir, $targetDir)        $files | Remove-Item}    }
Set-Alias <command alias here>-BackUp <global function> – Description “<description here>” -Scope Global

And here is the command for Restoring your script from the Backup.

Before running this command, do this command first! Install-Module PS-Menu

# Alias <alias here>
Function global:<global function here> {
$restorechoice = Menu @(Get-ChildItem -Path “<backup directory>” -Filter <$targetFilenameBase>-*.zip)
Write-Host “You have chosen $restorechoice for the restore”
Expand-Archive -LiteralPath $restorechoice -DestinationPath “<path to your script>” -Force

Set-Alias <command alias here> <global function> -Description “<Description here>” -Scope global

Leave a Reply:

Your email address will not be published. Required fields are marked *