Skip Ribbon Commands
Skip to main content

Quick Launch

Todd Klindt's home page > Todd Klindt's Office 365 Admin Blog > Posts > Using Windows PowerShell to backup your SharePoint 2010 webs
April 05
Using Windows PowerShell to backup your SharePoint 2010 webs

As an administrator I'm constantly finding ways to use Windows PowerShell in my day to day administrative activities. Today's PowerShell tip is no different. For a while now Shane and I have been demonstrating how to use a simple PowerShell loop to back up all the site collections in your farm. It looks like this:

Get-SPWebApplication | Get-SPSite | ForEach-Object{$FilePath = "C:\Backup\" + $_.Url.Replace("http://","").Replace("/","-") + ".bak" ; Backup-SPSite -Identity $_.Url -Path $FilePath}

Since I've demonstrated that one a couple of times I felt it would be cheating to use that as my PowerShell Tip O' The Week. A couple of weeks ago someone on the TechNet forums asked how to use PowerShell to back up all of their webs, not site collections. I thought this was a worthy challenge as it had a few different aspects to it. Here's what I ended up with:

Get-SPSite http://upgrade/* | Get-SPWeb -limit all | ForEach-Object { [string] $dt = Get-Date -Format yyyyMMdd; $filepath = "C:\Backup\" + $_.Url.replace("http://","").replace("-","--").replace("/","-") + "-$dt.export" ; Export-SPWeb $_ -path $filepath -IncludeUserSecurity -IncludeVersions all -UseSqlSnapshot}


You can see it's very similar to the first one, but with a few changes. First, I didn't want to back up the webs from all the web applications in the farm, so instead I seeded the command with "Get-SPSite http://upgrade/*" which gives all the webs in that web app instead. You can also use filters to control which webs are backed up. I made sure and used "-limit all" with "Get-SPWeb" to get all the webs instead of the default of 20. Next I changed now the file names were constructed. I added the date to them. This way you can run it daily and keep multiple versions. Since Get-Date outputs a datetime object I had to cast it as a string. The format string of yyyyMMdd means the date will come out as something that is sortable in the file system. Today's date of April 5th, 2010 will show up as 20100405 in that format. Next I build the filename the same way as the site collection example, but through the $dt variable at the end. You could also put it at the beginning instead, if you wanted all of your backups sorted by date instead of by web name. To get the value of $dt instead of the literal string $dt I used double quotes. They behave differently than single quotes. Finally I export the web. I had more options to consider, so I showed some options. For instance, I use –includeusersecurity to include security and I use =includeversions to include all versions. Just to show off a little, I throw in a –UseSqlSnapshot too. If you choose not to include the date in the filename you'll end up with an error as it will be trying to write to a file that already exists. Export-SPWeb does not have an –overwrite switch, but it does have a –force switch which remedies that situation.

Since SPWeb exports are not full fidelity you probably won't use them for actual disaster recovery. For instance, you will lose your user's Alerts and any workflows. However, you might have some need for a backup of all your webs. If you do, this is the script for you. Remember, you'll have to run this from the SharePoint 2010 Management Shell, or run "add-pssnapin Microsoft.sharepoint.powershell" in your PowerShell console. And of course you'll have to run this on a SharePoint server.

Thanks to Shane Young for the site collection script that started this, and thanks to Darrin Bishop for giving my PowerShell script a sanity check and suggesting some things I didn't think of.



does not back up site collections on application other than port 80

In running the script to back up all the site collections, it work fine for all the site collections on port 80.  I have My Sites hosted at http://mysites:3400 and can't see what i have to change to make this work. It just ignores these site collections.
 on 8/12/2010 11:16 AM

Re: does not back up site collections on application other than port 80

I'm guessing the colon in the filename is the problem. That character has a specific meaning in NTFS. I don't have any none 80 web applications to test this on, but try this and see if it works:

Get-SPWebApplication | Get-SPSite | ForEach-Object{$FilePath = "C:\Backup\" + $_.Url.Replace("http://","").Replace("/","-").Replace(":","-") + ".bak" ; Backup-SPSite -Identity $_.Url -Path $FilePath}

All I did was add ".Replace(":","-")" to the string that creates the filename.

Let me know if that helps.
Todd O. KlindtNo presence information on 8/12/2010 1:42 PM

loop to back up site collections

If i try and run your first script, looping through the site collections, more than once, it errors out. I actually have to delete the .bak files to make it work again. Is there an overwrite switch that can be used here?
Get-SPWebApplication | Get-SPSite | ForEach-Object{$FilePath = "C:\Backup\" + $_.Url.Replace("http://","").Replace("/","-") + ".bak" ; Backup-SPSite -Identity $_.Url -Path $FilePath}

 on 10/25/2010 12:58 PM

Re: loop to back up site collections

Use the "-force" parameter with Backup-SPSite to overwrite existing backup files.

Todd O. KlindtNo presence information on 10/25/2010 1:02 PM

Windows2008R2/SQL2008R2 problems

I try it on 2008 without any problems. But running on R2 versions the script will not work.
Are the know issues?
 on 12/24/2010 2:23 PM

Fixed the problem on R2 versions

Scripts are working but I used the wrong URL.
Used the external URL instead of the Default AAM URL
 on 12/25/2010 5:23 PM

Exclude a specific web app

I want to script this and have it run on a schedule weekly.   IS there a way to exclude a web app, say mysites.   I really don't want to backup all individual mysites weekly.    Any ideas?
 on 1/11/2011 10:09 AM

Add it to a batch file

Hi Todd,   How can I add this script to a batch file?    I can't seem to figure it out.   

 on 1/12/2011 12:15 PM

Re: Add it to a batch file

It can be done, but it's a little tricky. First, you need to make sure PowerShell's execution policy will allow scripts to be run. From a PowerShell prompt type "get-help about_Execution_Policies" to learn more about it. You can use this command "set-executionpolicy remotesigned" to set it at a level where you local script will run.

Next create a text file and save it as a .PS1 file. Something like farmbackup.ps1 will do. Put the PowerShell backup commands in there and save it. Finally you'll need to create .CMD file to run your PS1 file. Call it farmbackup.cmd. It needs to contain this line: "powershell -command "& 'farmbackup.ps1' "

Then schedule that .CMD with the Task Scheduler.

Nothing to it. :)


Todd O. KlindtNo presence information on 1/12/2011 10:27 PM

Re: Exclude a specific web app

You could wedge in a where-object command between the get-spwebapplication and the get-spsite.  You can also take advantage of get-spsite's -identity and -filter parameters to filter site collections or web applications out.

Todd O. KlindtNo presence information on 2/15/2011 10:24 PM
1 - 10Next

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.


Body *

Today's date *

Select a date from the calendar.
Please enter today's date so I know you are a real person


Want a message when I reply to your comment? Put your Twitter handle here.