This will backup your sharepoint sites to a given path and delete any backups older than 3 days old.
param(
$backupPath = $("You must enter a location to place the backup files")
)
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell
$fileDate = get-date -f yyyyMMdd
get-spwebapplication | get-spsite | %{$filePath = $backupPath + $_.URL.Replace("http://", "").Replace("/", "-") + "_" + $fileDate + ".bak" ; backup-spsite -identity $_.URL -path $filePath}
$files = get-childitem -path $backupPath | where{$_.Extension -eq ".bak"}
foreach($file in $files)
{
if($file.CreationTime -lt (get-date).addDays(-3))
{
remove-item $file.FullName -force
}
}
Leave a comment