Using PowerShell to produce a list of databases from a list of server\instances

Need to produce a report showing all the databases in your environment? Why not include the name, size and owner while we are at it and export it to a csv file. Here you go!

Note: The SQLPS module is installed on a machine with Microsoft SQL Server Management Studio. I have tested this with MSSMS 2014.

The PowerShell Script

import-module "C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\SQLPS" -DisableNameChecking
$rootdir = "C:\Users\VirtualRick\SQL Server Audit\"
$instances = import-csv $rootdir\server-instance.csv
ForEach($row in $instances)
{
$sqlPath = "SQLSERVER:\SQL\$($row.server)\$($row.instance)\Databases\"
dir $sqlPath | select Name, Size, Owner | export-csv $rootdir\export.csv -Append
}

 

server-instance.csv file example:

Server,Instance

MyServer,default

2 Replies to “Using PowerShell to produce a list of databases from a list of server\instances”

  1. on Verizon Samsung Galaxy S6 Edge – Data not available while on a call ineska on Networking not working after Windows 10 Update (version 1511, build 10586. Where is this information?

Leave a Reply

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