Retrieve Default SQL Server Backup Folder using PowerShell
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?
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.
- 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
Last Updated: 2010-04-15
About the author
View all my tips