Pages

Monday, June 1, 2009

list out the Databases in SQL Server using PowerShell

Today, I started to learn Windows PowerShell. Initially I thought it is difficult but by it surprised me completely. It’s very easy to learn in fact if you have knowledge of VBScript or .Net then it is much easy to learn.

Let’s look at the simple script that displays the Databases in SQL Server

PowerShell:
Step 1:
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') out-null

Step 2:
$sSQLServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "VIJAY_MACHINE\VJ_LCL"

VIJAY_MACHINE => is my Machine Name
VJ_LCL => is my SQL Server Instance Name

Step 3:
$SQLServerDatabases = $sSQLServer.Databases

Step 4:
$SQLServerDatabases Select Name,RecoveryModel,Size,SpaceAvailable

Output Is

1 comment:

Ronald S Woan said...

SQL Server 2008 has a PowerShell Add-in that provide a filesystem namespace for navigating databases, tables, users, etc...