PowerShell as a Backup Solution

This is a quick PowerShell (PS) way of creating backups for your documents.

I have another post that uses robocopy. I tend to like the robocopy solution more because it can be smarter about copying files only if they don’t already exist in the destination (the backup location).

On the backup location, place the code below on a text file named backup.ps1.

# the line below stops the execution of the file when a
# line errors out
$ErrorActionPreference = "Stop"

$folderName = ".\$((Get-Date).ToString('yyyy-MM-dd'))"

echo "---- Creating dated folder"
New-Item -ItemType Directory -Path $folderName

echo "---- Copying C:\home\docs into dated folder"
Copy-Item C:\home\docs $folderName -recurse -Force

Edit and add the lines that start with Copy-Item so they copy the folders you are interested in.

On the same folder that contains backup.ps1, create a text file named run-backup.bat and make it have these contents:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -File backup.ps1 > last-execution.log 2>&1

run-backup.bat makes it so

  • You don’t get an error saying “backup.ps1 cannot be loaded because running scripts is disabled on this system. See about_execution_Policies”
  • The output of backup.ps1 makes it to the last-execution.log file

One thought on “PowerShell as a Backup Solution

  1. Pingback: Robocopy as a backup solution | Pedro Liska

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s