General / Other Scripts

This category page is part of a series of Professional PowerShell SMO Recipes. You can find an index of all recipes on the main SMO Recipes page, and if you want to learn more about each recipe and see some sample screenshots, click on its category page.

These scripts were created and tested on Windows 8.1 with PowerShell v4 and SQL Server 2014, though most recipes should work with earlier versions of SQL Server. PowerShell v3 and above is required for many of the recipes. If you have a recipe request, leave a comment and I'll see what I can do. This cookbook will be continuously built, as I work more with SMO.

Recipe Categories

General / Other Scripts

Below I’ve included a couple SMO, and non-SMO bonus scripts that can be really useful for automated SQL Server management.

Return query with just one result

SqlCommand’s ExecuteScalar() is cool because it’s made to return just one result. Think of it as one column of one row.

The closest I can find to that in SMO is calling a single item in a single row using ExecuteWithResults().

Echo your connection string

For when you don’t want to look up the syntax on the Internet..

Get list of all user (non-system) databases

I’ve found SMO’s IsSystemObject to be super useful in a number of my scripts. It’s good to be aware of it.


Oddly enough $server.GetSmoObject($urn) didn’t work in this case. The Microsoft.SqlServer.Management.Smo.SmoException it produced was: For the moment we don’t support Urn’s that do not start with Server

So I had to get creative when traversing backwards (as seen in “Get CMS Servers and Groups“)

Populate Dynamic Parameter with database names

This is one of my favorite and most used snippets; it will build a new Dyanamic Parameter named Databases which makes -Databases tabbable from the command line. For example: .\Test-Code.ps1 -ServerName sqlserver -Databases master. Or you can specify multiple databases by using a comma: -Databases master, otherdb.

To limit database selection to just one, modify the code below to reflect (“Databases”, [String], $dbattributes) instead of (“Databases”, [String[]], $dbattributes).

Populate Dynamic Parameters with database and login names

The above code is highly reusable for other Dynamic Parameters, as well. The script below will generate a list of all user databases and logins, and then add them to the dynamic parameters IncludeLogins, ExcludeLogins, IncludeDBs, ExcludeDBs.

Want to see more? You can find an index of all recipes on SMO Recipes Index Page page or click on any specific category at the top of this page.

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Leave a Reply

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