One of the most pleasing things about Sharepoint is the ability to do the same administrative action in a multitude of ways: through the central administration UI, Powershell, or using the object model. Powershell is not only the indisputably most preferred administrative way, but for some actions, it is the only way.
In this example, I will show a script that uses the power of Powershell pipelines to backup all the site collections.
The method is simple, using the Get-SPSite command; I get all the site collections. After I iterate in the collection using For-EachObject. For each collection, I build a backup path by using the site URL and by replacing the symbols by “_”. After, I backup each site using the command Backup-SPSite. Notice that I use the $_ alias which is very useful to manipulate objects in iterations.
This is the simple script that expresses all the power of Powershell
get-spsite -Limit All | ForEach-Object {$path = $_.Url.Replace("/","_").Replace(":","_"); $path="c:\backup_path\$($path).bak"; Write-Host "Backing the collection $($_) up using the path $($path)...."; Backup-SPSite $_ -Path $path; Write-Host "The backup of the collection $($_) has been performed successfully"}
Enjoy