≡ 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 {
[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…

{ 1 comment… add one }
  • Sal Young April 14, 2011, 7:05 am

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

Leave a Comment