Home > PowerShell, SQL > PowerShell Query databases list from SQL server

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…

Categories: PowerShell, SQL Tags: ,
  1. April 14, 2011 at 7:05 am | #1

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

  1. No trackbacks yet.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>