Backup Sharepoint Sites with Powershell

This will backup your sharepoint sites to a given path and delete any backups older than 3 days old.

    $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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.