PowerTip: Use PowerShell to Back Up System Databases

Doctor Scripto

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}}


Discussion is closed.

Feedback usabilla icon