PowerTip: Use PowerShell to Back Up System Databases


Summary: Use Windows PowerShell to back up system databases.

Hey, Scripting Guy! Question How can I use Windows PowerShell to dynamically back up all of the system databases on my servers?

Hey, Scripting Guy! Answer Combining provider lookups with the Backup-SqlDatabase cmdlet makes this simple:

$instances = @(‘KIRK’,’SPOCK’,’PICARD’,’RIKER’)

foreach($instance in $instances){

            $dbs = Get-ChildItem SQLSERVER:\SQL\$instance\DEFAULT\Databases -Force |

Where-Object {$_.IsSystemObject -eq $true -and $_.Name -ne 'TempDB'}

    $dbs |

ForEach-Object {

Backup-SqlDatabase -ServerInstance $instance -Database $_.Name -BackupFile "C:\DBFiles\$($_.Name).bak" -Initialize }}

$instances |

ForEach-Object {Invoke-Command -ComputerName $_ -ScriptBlock {Get-ChildItem C:\DBFiles\*.bak}}

Comments (0)

Skip to main content