{"id":4794,"date":"2012-10-21T00:01:00","date_gmt":"2012-10-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/10\/21\/weekend-scripter-easily-back-up-all-sql-server-databases-by-using-powershell\/"},"modified":"2012-10-21T00:01:00","modified_gmt":"2012-10-21T00:01:00","slug":"weekend-scripter-easily-back-up-all-sql-server-databases-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-easily-back-up-all-sql-server-databases-by-using-powershell\/","title":{"rendered":"Weekend Scripter: Easily Back Up All SQL Server Databases by Using PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use the SQL Server 2012 provider for Windows PowerShell to easily back up all databases on an instance.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I am still playing around with SQL Server&nbsp;2012, and, today, I want to check on my backups by using Windows PowerShell.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;This is the fourth article in a series of articles discussing using Windows PowerShell to manage SQL Server&nbsp;2012. You should read the first three articles in this series prior to reading this article. In the&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/10\/18\/introduction-to-the-sql-server-2012-powershell-provider.aspx\" title=\"first article\" target=\"_blank\">first article<\/a>, I introduced the SQL Server&nbsp;2012 provider for Windows PowerShell. In the&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/10\/19\/use-powershell-to-work-with-the-sql-server-object.aspx\" title=\"second article\" target=\"_blank\">second article<\/a><i>, <\/i>I talked about the properties and methods that become available once you store the instance in a variable. In the&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/10\/20\/weekend-scripter-use-powershell-to-work-with-sql-server-2012-database-objects.aspx\" title=\"third article\" target=\"_blank\">third article<\/a>,&nbsp;I talked about using Windows PowerShell to work with SQL Server database objects.<\/p>\n<h2>The easy way to find the last backup of a SQL database<\/h2>\n<p>It is extremely easy to find the last backup dates of a SQL Server&nbsp;2012 database by using the Windows PowerShell provider. I enter my <b>db<\/b><i> <\/i>drive, type <b>dir<\/b><i>, <\/i>and then choose the appropriate properties.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;The <b>db<\/b><i> <\/i>drive refers to the databases collection node and was created yesterday.<\/p>\n<p>The command and associated output are shown here.<\/p>\n<p style=\"padding-left: 30px\">[sql1]: PS db:\\&gt; dir | select name, *backupdate | fl<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Northwind<\/p>\n<p style=\"padding-left: 30px\">LastBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/15\/2012 7:48:26 PM<\/p>\n<p style=\"padding-left: 30px\">LastDifferentialBackupDate : 1\/1\/0001 12:00:00 AM<\/p>\n<p style=\"padding-left: 30px\">LastLogBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/15\/2012 8:09:39 PM<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : pubs<\/p>\n<p style=\"padding-left: 30px\">LastBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/15\/2012 7:48:26 PM<\/p>\n<p style=\"padding-left: 30px\">LastDifferentialBackupDate : 1\/1\/0001 12:00:00 AM<\/p>\n<p style=\"padding-left: 30px\">LastLogBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/15\/2012 8:09:39 PM<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ReportServer$INSTANCE_A<\/p>\n<p style=\"padding-left: 30px\">LastBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/15\/2012 7:48:26 PM<\/p>\n<p style=\"padding-left: 30px\">LastDifferentialBackupDate : 1\/1\/0001 12:00:00 AM<\/p>\n<p style=\"padding-left: 30px\">LastLogBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/15\/2012 8:09:40 PM<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ReportServer$INSTANCE_ATempDB<\/p>\n<p style=\"padding-left: 30px\">LastBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/15\/2012 7:48:26 PM<\/p>\n<p style=\"padding-left: 30px\">LastDifferentialBackupDate : 1\/1\/0001 12:00:00 AM<\/p>\n<p style=\"padding-left: 30px\">LastLogBackupDate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1\/1\/0001 12:00:00 AM<\/p>\n<h2>Back up the database<\/h2>\n<p>To back up every database on my specific instance (the one to which the <b>db<\/b><i> <\/i>drive points), I use the <b>get-ChildItem<\/b> cmdlet (<b>dir<\/b> is the alias) and pipe the results to the <b>Backup-SqlDatabase<\/b> cmdlet as shown here.<\/p>\n<p style=\"padding-left: 30px\">dir | Backup-SqlDatabase<\/p>\n<p>To back up the transaction log for a specific database, I use the <b>Backup-SqlDatabase<\/b> cmdlet, but specify a <b>BackupAction<\/b><i> <\/i>of <b>log<\/b><i>. <\/i>I also need to specify the <b>serverInstance<\/b> as well as the database name. The following command backs up the transaction log for the <b>pubs<\/b><i> <\/i>database.<\/p>\n<p style=\"padding-left: 30px\">Backup-SqlDatabase -serverInstance sql1\\Instance_a -BackupAction log -Database pubs<\/p>\n<p>If I want to back up the transaction logs for all of the databases on the <b>Instance_a<\/b> instance of my sql1 server, I can use the <b>Get-ChildItem<\/b> cmdlet to obtain a listing of all of the databases, and then pipe the results to the <b>Foreach-Object<\/b> cmdlet.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;Unfortunately, the <b>Backup-SqlDatabase<\/b> cmdlet does not appear to accept piped input when backing up transaction logs.<\/p>\n<p>The following code backs up the transaction logs for all databases that do not have a simple recovery method (an expected error generates for databases in simple recovery mode).<\/p>\n<p style=\"padding-left: 30px\">$db = dir<\/p>\n<p style=\"padding-left: 30px\">$db.name |<\/p>\n<p style=\"padding-left: 30px\">foreach {Backup-SqlDatabase -serverInstance sql1\\Instance_a -BackupAction log -Database $_ -ea 0}<\/p>\n<p>To check the last backup date and recovery model, I pipe the database objects to the <b>Select-Object<\/b> cmdlet and display the <b>RecoveryModel<\/b><i> <\/i>and the <b>LastBackupDate<\/b> for the database as shown here.<\/p>\n<p style=\"padding-left: 30px\">[sql1]: PS db:\\&gt; dir | select name, recoverymodel, lastbackupdate<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RecoveryModel LastBackupDate<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">Northwind&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Full 10\/15\/2012 7:48:26 PM<\/p>\n<p style=\"padding-left: 30px\">pubs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Full 10\/15\/2012 7:48:26 PM<\/p>\n<p style=\"padding-left: 30px\">ReportServer$INSTANCE_A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Full 10\/15\/2012 7:48:26 PM<\/p>\n<p style=\"padding-left: 30px\">ReportServer$INSTANCE_ATe&#8230;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Simple 10\/15\/2012 7:48:26 PM<\/p>\n<p>To view the backup set files, I use the <b>EnumBackupSetFiles<\/b><i> <\/i>method from the specific database that I am interested in working. The following code retrieves the <b>pubs<\/b><i> <\/i>database object, and then enumerates the backup set files.<\/p>\n<p style=\"padding-left: 30px\">$pubs = Get-Item pubs<\/p>\n<p style=\"padding-left: 30px\">$pubs.EnumBackupSetFiles()<\/p>\n<p>The code and the output associated with the code are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4722.hsg-10-21-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4722.hsg-10-21-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That&rsquo;s about it for messing around with the SQL Server&nbsp;2012 database backup. Join me tomorrow when I will kick off a new week and show you the easy way to work with the SQL Error Log.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use the SQL Server 2012 provider for Windows PowerShell to easily back up all databases on an instance. Microsoft Scripting Guy, Ed Wilson, is here. I am still playing around with SQL Server&nbsp;2012, and, today, I want to check on my backups by using Windows PowerShell. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[146,3,176,375,61,45],"class_list":["post-4794","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-databases","tag-scripting-guy","tag-sql-server","tag-sql-server-2012","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use the SQL Server 2012 provider for Windows PowerShell to easily back up all databases on an instance. Microsoft Scripting Guy, Ed Wilson, is here. I am still playing around with SQL Server&nbsp;2012, and, today, I want to check on my backups by using Windows PowerShell. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4794"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4794\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}