Techibee.com

PowerShell Query databases list from SQL server

Below small function helps you to list databases in a given database server using powershell. This will come handy for you when you quickly want to check the databases list without logging on to the server.

function Get-SQLDatabases {
param(
[Parameter(mandatory=$true)]
[string]$DBServer
)
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’) | Out-Null

$server = New-Object (‘Microsoft.SqlServer.Management.Smo.Server’) “$DBServer”
$server.databases | select Name

}

Usage :

C:>Get-SQLDatabases -DBServer MSSQLSRV1

Hope this helps…

Exit mobile version