Basic PowerShell Syntax for Export-SPWeb
-
Saturday, March 20, 2010 2:46 PM
I'm trying to write a simple PowerShell script that will back up each web site within a site collection. The problem I am having is trying to get the PowerShell variable substitution working for my Path parameter. Here is what I am trying to do
get-spweb -site http://sp2010 | export-spweb -path "c:\backup\$_.Name.cmp"
The problem is that $_.Name is being taken literally in the filename, and what I want is to take the Name property of each SPWeb and have that become the filename so that each backup file name is unique. I'm still warming up with PowerShell, so I'm sure there's a silly little thing I'm missing.
Any guidance?
Randy - http://sharepointhawaii.com/randywilliams
Answers
-
Monday, March 22, 2010 4:37 AM
Hi Randy,
You need something like this:
Get-SPWeb -site http://sp2010 | ForEach-Object { $filename = $_.Url.replace("http://","").replace("-","--").replace("/","-") + ".export" ; Write-Host export-spweb $_ -path $filename}
That will walk through all the webs under sp2010, remove the "http://" at the beginning, replace all the dashes with doubledashes, and the slashes with dashes. Then exports them. That way something like http://sp2010/sites/test/foo-bar will be savesd as sites-test-foo--bar.export.
Oh, and you'll have to remove the "write-host" for it to actually work. I put that in as a safety measure. :)
Hope that helps,
tk
- Marked As Answer by Randy.Williams Monday, March 22, 2010 4:55 AM
All Replies
-
Monday, March 22, 2010 4:37 AM
Hi Randy,
You need something like this:
Get-SPWeb -site http://sp2010 | ForEach-Object { $filename = $_.Url.replace("http://","").replace("-","--").replace("/","-") + ".export" ; Write-Host export-spweb $_ -path $filename}
That will walk through all the webs under sp2010, remove the "http://" at the beginning, replace all the dashes with doubledashes, and the slashes with dashes. Then exports them. That way something like http://sp2010/sites/test/foo-bar will be savesd as sites-test-foo--bar.export.
Oh, and you'll have to remove the "write-host" for it to actually work. I put that in as a safety measure. :)
Hope that helps,
tk
- Marked As Answer by Randy.Williams Monday, March 22, 2010 4:55 AM
-
Monday, March 22, 2010 4:54 AM
Works like a charm. Thanks. Hadn't thought of using ForEach-Object. I modifed slightly to also work to backup all site collections for a designated web application for those that can use it.
Get-SPWebApplication "SharePoint - 80" | Get-SPSite -limit all | ForEach-Object { $filename = "\\server\share\" + $_.Url.replace("http://","").replace("_","__").replace("/","_") + ".export" ; backup-spsite $_.Url -path $filename}
Randy - http://sharepointhawaii.com/randywilliams

