Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Powershell Backup And Copy
# BackupAndCopy.psm1
function BackupAndCopy
param([string]$source, [string]$destination, [string]$backup)
$backupPath = $("$backup\") + $(Split-Path $destination -Leaf) + "_"+ $(get-date -Format MM-dd-yyy_HH-mm-ss) + "\"
New-Item -ItemType Directory -Path $destination$Suffix -Force
New-Item -ItemType Directory -Path $backupPath -Force
New-PSDrive -Name src -PSProvider FileSystem -Root $source -Credential $credentials
New-PSDrive -Name dest -PSProvider FileSystem -Root $destination$Suffix
New-PSDrive -Name backup -PSProvider FileSystem -Root $backupPath
echo "Backing up $destination to excluding the Web.config"
Copy-Item -Path dest:\* -Destination backup:\ -Recurse -Force -PassThru -Container
echo "Copying files from $source to $destination excluding the Web.config"
Copy-Item -Path src:\* -Destination dest:\ -Exclude @('Web.config') -Recurse -Force -PassThru -Container
echo "Delete folders/files older "
$limit = (Get-Date).AddDays(-7)
Get-ChildItem -Path $backup -Force -Directory | Where-Object { $_.CreationTime -lt $limit } | Remove-Item -Force -Recurse -Verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment