PowerShell – Script to Listing all Backups Taken with SQL Server

I always like to keep a number of scripts handy in my USB drive with my Laptop. These scripts are used for my internal use when I am suddenly looking for something interesting in customer environments. One of the scripts that I use is shown below. Many a times I have seen customers don’t really have any backup strategy. And when I start working with them, I call out some of the basic backup techniques that they need to be doing.

I have learnt and advised on a number of occasions that the backup strategy is not to be thought about in isolation but needs to be built keeping the restore strategy in mind. Your restore strategy and the RTO, RPO conversation with the stakeholders will decide how a typical backup plan needs to be done.

Let me get back to the fundamental problem statement that I wanted to share. The below powershell script is used to identify the backups taken for each of the databases.

PowerShell Script to Listing all Backups

</gs>Import-Module SQLPS
$ServerName = "BigPinal"    #"Replace with your <gs id="6563431d-bbce-4739-9279-780f3366dd87" ginger_software_uiphraseguid="72e9cd3c-57f5-4072-9401-0a73c9fb23cf" class="GINGER_SOFTWARE_mark">servername</gs>"
$InstanceName = "Default"  #"Default"
$ConnectionString = "SQLSERVER:\SQL\"+$ServerName+"\"+$InstanceName+"\Databases"
<gs id="639d042d-12fa-46c2-ab73-7c5076cd39a5" ginger_software_uiphraseguid="72e9cd3c-57f5-4072-9401-0a73c9fb23cf" class="GINGER_SOFTWARE_mark">dir</gs> $ConnectionString | %<gs id="7c3d4b49-704c-482d-a88e-55639226824a" ginger_software_uiphraseguid="72e9cd3c-57f5-4072-9401-0a73c9fb23cf" class="GINGER_SOFTWARE_mark">{</gs>$_<gs id="b5a1fed9-8b3c-4f2f-b649-d7b8f341d772" ginger_software_uiphraseguid="72e9cd3c-57f5-4072-9401-0a73c9fb23cf" class="GINGER_SOFTWARE_mark">.</gs>EnumBackupSets<gs id="baa4cdce-97d3-4780-b666-bb4ce951ec74" ginger_software_uiphraseguid="72e9cd3c-57f5-4072-9401-0a73c9fb23cf" class="GINGER_SOFTWARE_mark">(</gs>)} | ft 

The output for the same would look like below:

PowerShell - Script to Listing all Backups Taken with SQL Server backups-taken-ps-01-800x161

You might be of the opinion on why you would use. This is a simple yet powerful script you can use to initially take a dump to understand the various backups. I take a quick look at this output with the list of databases to understand if there are databases which are not being backed up or if there is a plan for backups or not.

I know this is a simple task, but you will be surprised to how many still make basic mistakes in their environments. Do let me know what have you got from your environments and what were some of your findings? I am sure want to learn from you too.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Powershell, SQL Backup, SQL Scripts, SQL Server
Previous Post
SQL SERVER – How to Avoid Triggers for Multiple Row Operations in a Table
Next Post
SQL SERVER – FIX Error 5120 – Database is in Read Only Mode After Attaching Files

Related Posts

Leave a Reply