Last active
September 10, 2016 03:28
-
-
Save ryanvgates/af08106279e83add8cc6f7e7c65ba064 to your computer and use it in GitHub Desktop.
Powershell Backup And Copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# BackupAndCopy.psm1 | |
# | |
function BackupAndCopy | |
{ | |
[cmdletbinding(SupportsShouldProcess=$True)] | |
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