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

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

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

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
- Download and install Windows PowerShell
- Read more on the SMO Class Library to translate SQL Server objects that we are already familiar with to SMO objects
- Check out the other Windows PowerShell tips

Edwin M. Sarmiento is the Managing Director of 15C, a consulting and training company that specializes on designing, implementing and supporting SQL Server infrastructures. He is a Microsoft SQL Server MVP and Microsoft Certified Master from Ottawa, Canada specializing in high availability, disaster recovery and system infrastructures running on the Microsoft server technology stack ranging from Active Directory to SharePoint and anything in between. He is very passionate about technology but has interests in music, professional and organizational development, and leadership and management matters when not working with databases. Edwin lives up to his primary mission statement: “To help people and organizations grow and develop their full potential.”
- MSSQLTips Awards: Champion Award (100+ tips) – 2017 | Author of the Year Contender – 2020, 2017