PowerShell: Obtaining SQL Database Default Paths

# This function returns an array of 3 string values reflecting default Data, Log, and Backup directories as set on a certain SQL server
function getDefaultSqlPaths($sqlServer=$env:computername){
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
    $sqlConnection = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $sqlServer 
    $defaultBackupDirectory=$sqlConnection.Settings.BackupDirectory
    $defaultDataDirectory=$sqlConnection.Settings.DefaultFile
    $defaultLogDirectory=$sqlConnection.Settings.DefaultLog
    return @($defaultDataDirectory,$defaultLogDirectory,$defaultBackupDirectory)
    }
getDefaultSqlPaths

Leave a Reply

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