Creating Backup Copies of Many Files

by Nov 24, 2015

PowerShell can quickly create backup files of your files. All you need to adjust is the type of files you want to backup, and the file extension you want to use for your backup files.

This example would affect all PowerShell scripts stored (directly) in your user profile. There are likely to be none in this location, so either copy one there to test drive the function, or specify a different folder path.

Each script would be backuped to a file with the same name and the extension ".ps1_old".

When you change the value of $Recurse to $true, the script would create backups of all PowerShell scripts anywhere in your user profile.

#requires -Version 1

$ExtensionToBackup = '.ps1'
$BackupExtension = '.ps1_old'
$FolderToProcess = $HOME
$Recurse = $false


Get-ChildItem -Path $FolderToProcess -Filter $ExtensionToBackup -Recurse:$Recurse |
  ForEach-Object { 
    $newpath = [System.IO.Path]::ChangeExtension($_.FullName, $BackupExtension)
    Copy-Item -Path $_.FullName -Destination $newpath
 }

Twitter This Tip! ReTweet this Tip!