≡ Menu

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…

Comments on this entry are closed.

  • Sal Young April 14, 2011, 7:05 am

    You should look into SQLPSX in codeplex. It will enhance your PowerShell development experience.