Submitted By: Ryan M. Rinehart

Uses SQL 2005 SMO Objects to lists all the databases on a SQL Server computer.

PowerShell
Edit|Remove
# ===================================================================================================================           
# AUTHOR      :               Ryan M.  Rinehart  MCSE, MCDBA
#
# EMAIL       :               rrinehart77@hotmail.com
# 
# NAME        :               SMOListDB.ps1
#
# VERSION     :               1.0
#
# DATE        :               7/16/2007
#
# DESCRIPTION :               Uses SQL 2005 SMO to list the version and databases running on the SQL Server specified
# 
#
# 
# COMMENT(s)  :               SQL Server must support SMO for this to function properly - 8.0 + 9.0
# 
#
# ===================================================================================================================

# Path to the SDK assemblies
$sPath = [string] "C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies"

# SQL Server to connect to
$sSqlSrv = [string] "SQL2005Test"

# load the assemblies needed for SQL SMO from the SDK
[void][System.Reflection.Assembly]::LoadFile($sPath + "\Microsoft.SqlServer.ConnectionInfo.DLL") 
[void][System.Reflection.Assembly]::LoadFile($sPath + "\Microsoft.SqlServer.SMO.DLL") 
[void][System.Reflection.Assembly]::LoadFile($sPath + "\Microsoft.SqlServer.SMOEnum.DLL") 

# new-up a SQL Server SMO Server object reference
$oSrv = New-Object Microsoft.SqlServer.Management.Smo.Server($sSqlSrv)

# write SQL Version to console + line return

Write-Host $sSqlSrv "runs SQL Server Version " $oSrv.Information.Version
Write-Host "`r"
Write-Host "Found the following Databases on " $sSqlSrv ":"
Write-Host "`r"

# enumerate Databases on this server
foreach($oDb in $oSrv.Databases)
{
                # write database name/ID  to console
                Write-Host "Database: " $oDb.Name "`t" "DBID " $oDb.ID
}