SQL Server PowerShell Extensions

There are several different approaches to working with SQL Server from PowerShell; among them are WMI, the Provider; cmdlets, SMO, and ADO.NET. The SQL Server Provider and its resulting PSDrive provide some easy ways to get to information inside of your SQL Server instance. The SQL Server cmdlets that come with SSMS 2012 enable you to do some basic tasks such as running queries and backing up databases. On the other end of the spectrum (for the vast majority of DBAs) is the need to learn and leverage the SMO.

The easiest way to get comprehensive abilities to work with SQL Server from PowerShell is to download the SQL Server PowerShell Extensions project from CodePlex ( http://sqlpsx.codeplex.com ). As of this writing SQLPSX comes with 163 advanced functions, 2 cmdlets, and 7 scripts. The download includes an installer to streamline the process. If you want to (or need to for security reasons) you can download just the source code, which is simply a bunch of text files.

Importing Modules

Starting with PowerShell 2.0 you can extend the native capabilities of PowerShell by importing modules. With SSMS 2012 the PowerShell functionality of SQL Server is finally available in module form. When working with the relational engine and its various components, you use the command IMPORT-MODULE SQLPS. When working with SSAS you use IMPORT-MODULE SQLASCMDLETS.

You need to import only a module once per PowerShell session; however, if you do accidentally import ...

Get Microsoft SQL Server 2012 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.