Why PowerShell for SQL Server?

Learn why PowerShell is a valuable addition for managing SQL Server

I have to admit that the issue of whether or not to use PowerShell to manage SQL Server is something I have pondered ever since Microsoft first integrated PowerShell with SQL Server 2008. The initial PowerShell integration with SQL Server 2008 used a mini-shell that was of limited use. Then, with SQL Server 2012, Microsoft provided a fully capable PowerShell environment, making PowerShell more useful by letting you add other snap-ins. However, scripted management for SQL Server is nothing new. T-SQL has been an integral management tool for SQL Server since the beginning.

Why should you use PowerShell to manage SQL Server? To help answer that question, I checked out some of the TechNet articles in which Microsoft illustrates the "amazing power" of being able to list instances and enumerable database objects—all with a few lines of code. According to these articles, you can automate operations by running scripts and even execute T-SQL scripts. Huh? I feel bad about pointing this out, but SQL Server Management Studio (SSMS) enumerates SQL Server objects almost instantly with no code, and T-SQL stored procedures and batches already provide automation. There has to be something more to encourage SQL Server professionals to use PowerShell. 

Pros and Cons

What are the pros and cons to using PowerShell to manage SQL Server? I think we probably know the cons best. PowerShell is foreign to most SQL Server professionals and there's a substantial learning curve if you don't have a developer background. Because I have a developer background, PowerShell has always come naturally to me—but at the same time, I recognize that PowerShell has all of the ingredients that make it difficult for non-developers to learn. Plus, SQL Server already possesses a scripted management environment with T-SQL—one that most SQL Server professionals are aware of and are proficient at using. So, PowerShell potentially adds more complexity into the day-to-day operating environment.

Learn more: SQL Server and PowerShell FAQs

However, PowerShell for SQL Server does have real pros, as well. On the pro side, PowerShell is becoming more central to the Windows management model and can provide a common framework for managing all the different servers in a Microsoft Windows infrastructure. PowerShell can manage Windows, Exchange, SharePoint, and SQL Server. PowerShell is also an advanced .NET-based object oriented language that is arguably the most powerful scripting environment there is. Unlike T-SQL, PowerShell can access resources that are outside of SQL Server. PowerShell is also capable of performing advanced math and a multitude of other functions that are supported in the .NET Framework—far beyond T-SQL. PowerShell is also capable of running T-SQL queries using the Invoke-Sqlcmd cmdlet.

PowerShell Bridges SQL Server/OS gap

Although I don't see any SQL Server professionals dropping T-SQL or SSMS in favor of PowerShell, I  think that PowerShell is a valuable addition for managing SQL Server. PowerShell's ability to bridge the SQL Server/OS gap and access systems and network resources, both inside and outside of SQL Server, is something that T-SQL can't do. One of the things that I use PowerShell for is to run sets of SQL Server benchmarks where a driver script executes and times a set of queries from multiple clients to multiple SQL Server systems—that can't be done with just T-SQL, and PowerShell provides a far more robust platform than Windows Shell scripts.

What exactly are the PowerShell integration components for SQL Server? There are two PowerShell snap-ins for SQL Server 2008 and 2012: a SQL Server provider named sqlps and a set of SQL Server cmdlets. The sqlps PowerShell provider lets you navigate through a SQL Server instance and its objects exactly like you would navigate a standard file system. You can use the methods of properties of the different objects to manage them. The cmdlets have a verb-noun naming convention and they perform single-function commands. Some cmdlets include Add-SqlAvailabilitiyDatabase, Join-SqlAvailabilityGroup, Backup-SqlDatabase, Restore-SqlDatabase, and Invoke-Sqlcmd. At this point, the cmdlets are primarily focused on Availability Groups.

PowerShell can be a valuable SQL Server management tool, but learning it isn't always easy. For more information about PowerShell and SQL Server 2008, check out "SQL Server PowerShell Overview." Another good resource is "Understanding and Using PowerShell Support in SQL Server 2008." For some practical scripts, visit Bucky Woody's blog.

Discuss this Article 2

marc_jellinek@h...
on Feb 23, 2013
>>Microsoft illustrates the "amazing power" of being able to list instances and enumerable database objects How would you list instances on a server using only T-SQL?
ocolamatteo
on Feb 21, 2013
I tend to agree that with T-SQL we cannot accomplish as much, as elegantly, is as little code, as the things we can accomplish using PowerShell when it comes to bridging the "SQL Server/OS Gap" as you put it. However, you would have to pry T-SQL from the cold, dead hands of many of the DBAs I speak with about picking up PowerShell. For the ones who are open to the idea using PowerShell they tend to revert back to using their hammer, i.e. T-SQL and xp_cmdshell, even when the problem at hand may be presenting itself as requiring a jigsaw. If there were more examples online or in print geared towards what to use PowerShell for and how to use it effectively to manage SQL Server I think there would be far more adoption but the depth and breadth of resources just do not exist at the moment. It's actually something I have my sights set on to personally contribute to the community in 2013: blog more about how to use PowerShell to admin SQL Server and make some of the homegrown tools I use available.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.