Tuesday 2 August 2011

Powershell backup folder

Script purpose
Create a copy of a folder appending the current date and time to the newly created folder.

Script notes
Script is straight forward. A new folder (named FolderName-backupyyyyMMdd-hhmm) is created in the same path as the source folder. All the files are copied from the source to the new folder.

I use this to create a backup of my scripts on my dev pc before I sign and deploy them.

function Backup-Folder([string]$FolderPath)
{
<#
    .Synopsis
        Creates a back up copy of a folder
    .Description
    .Example
        Backup-Folder "C:\temp\folder1"
        Creates a folder "C:\temp\folder1-backupyyyyMMdd-hhmm"
    .Parameter FolderPath
        Path of folder that will be backed up
    .Link
        http://heazlewood.blogspot.com/
#>
  if (test-path -LiteralPath $FolderPath) {
 $FolderToCopy = Get-Item $FolderPath
 $newPath = Join-Path "$($FolderToCopy.Parent.FullName)" "$($FolderToCopy.Name)-backup_$((get-date).toString('yyyyMMdd-hhmm'))"
    
 Write-Host "copy $($FolderToCopy.FullName) to $newPath"
    
 copy -LiteralPath $FolderPath -Destination "$newPath" -Recurse -Force
  }
}

No comments:

Post a Comment