Retrieve Default SQL Server Backup Folder using PowerShell

Problem

In a previous tip on creating a Function to Return Default SQL Server Backup Folder , you’ve seen how you can create a T-SQL function to query the registry and retrieve the default SQL Server Backup folder. Is there an easier way to do it in Windows PowerShell?

Solution

Leveraging on what we already know from the tip on Using PowerShell with SQL Server Management Objects (SMO), we can access the different members of the Server object by running the Get-Member cmdlet

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’) | out-null
$s = New-Object (‘Microsoft.SqlServer.Management.Smo.Server’) “LOCALHOST”
$s | Get-Member -MemberType Property

Get-Member PowerShell Cmdlet

One particular property to note is the Settings property which we will use to retrieve modifiable settings of our SQL Server instance. We can then retrieve the different Settings property to list the default SQL Server Backup folder


$s.Settings | Get-Member -MemberType Property

$s.Settings | Get-Member -MemberType Property

The BackupDirectory property gives us the default SQL Server Backup directory value. This property can be modified as well but for our purpose, retrieving it would be enough.


[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’) | out-null
$s = New-Object (‘Microsoft.SqlServer.Management.Smo.Server’) “LOCALHOST”
$s.Settings.BackupDirectory

Find the SQL Server backup directory path in PowerShell

Notice how we have highlighted the power and ease of use of Windows PowerShell with SMO where, in three lines of code, we managed to retrieve the default SQL Server Backup folder. Applying what we have learned from previous Windows PowerShell tips with SMO, we can simply pass a list of SQL Server instances from any data source – text file, Excel spreadsheet, etc. – to retrieve this property across the enterprise.

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *