Performing a Database Backup

Another example that may be useful is performing a database backup using SMO from the PowerShell 2.0 console. The easiest way to load the SMO assemblies that will be used to perform the backups is to use the Import-Module cmdlet to import the SQLPS module. The module loads and registers the SQL Server snap-ins and manageability assemblies. Using the AdventureWorks2012 database, you can back up the database using just a few lines:

Import-Module "SQLPS" -DisableNameChecking$server=New-Object Microsoft.SqlServer.Management.Smo.Server$backup=new-object Microsoft.SqlServer.Management.Smo.Backup$file=new-object Microsoft.SqlServer.Management.Smo.BackupDeviceItem$file.Name="C:\backup\AW_DB.bak" ...

Get Microsoft® SQL Server 2012 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.