SQLServerCentral Article

PowerShell Basics


My definition of what PowerShell is: Command Prompt after a few cups of espreso.

See here to get an idea of how to start the PowerShell console. Windows 7 and Window Server 2008 R2 come with PowerShell already loaded for your exploration. If you have a lower operating system check this KB article to get it up and running.

I just want to address two things with this article: 1) How is PowerShell useful and 2) What PowerShell can do. I will provide some good links I have used to learn more about PowerShell that will hopefully help you as well.

How is PowerShell useful

Microsoft is integrating PowerShell into everything. It is either used for advanced tasks or does the background work for the GUI interface. If you have worked with SQL Server 2008 or SharePoint 2010 you will already notice it. For most DBAs or System Administrators it is most useful in getting consistency in your environment and deploying things across multiple servers more effeciently. You all know what you used VBScript for, now just think of that except with a ton more potential of saving you time. PowerShell can be used to manage SSIS packages across mutliple SQL Server instances to deploying a SharePoint 2010 farm in a mutli-tier environment.

The best way to tell you how useful something is, is by showing you. I had to get help from another forum on this one (here), but the command will go through each sub-folder and file of your current directory and spit out the owner of each.

Get-ChildItem -Recurse | ForEach-Object {$ACL = $(Get-Acl $_.PSPath).Owner; Write-Host "$($_.FullName) >> " -NoNewLine -ForegroundColor Green; Write-Host "$ACL" -ForegroundColor Red;}

What PowerShell can do

There are two options on how to interact with PowerShell:

1) PowerShell Prompt

2) PowerShell ISE (Integrating Scripting Environment)

The ISE is obviously geared more toward writing scripts and such. It is actually a pretty good editor to start out with learning PowerShell.

There are a ton of things PowerShell can do. To start off PowerShell commands follow a Verb-Noun format. So the most common commands you use follow "Get-Something" formatting. The examples I have is a quick way of checking the latest events in the event log in Windows and some things you can do with the help command.

Get-EventLog -LogName Application -Newest 5

This command is going to "get" the Application Event Log from Windows, showing the 5 newest events that have occured.

Say you want to find the other things you can do with the Get-EventLog command you would use Get-Help. A few of the parameters that come with Get-Help that are useful are -Full and (the one I like) -Examples. You also have -Online, which if you are on a computer with Internet access will take you to the MSDN article on the command in question.

Get-Help Get-EventLog -Examples

On most occassions about 10 or so examples come with each command, you may find a few that have less or more.

Another option with help is searching for a command. Say you know the noun of what you want and want to find what commands are available.

Get-Help *processes


There are a ton of sites out there that can help get you started with PowerShell. The following are just a few that I found most useful.

SSC articles and blogs on PowerShell

SSC PowerShell Forum

Automating Admin Tasks with Powershell (Free video from SQLBits.com, good intro into PowerShell)

Windows PowerShell Owner's Manual (good place to start, get you familar with the command line)

SQL Server Books Online: Running SQL Server PowerShell

SQL Server Books Online: SQL Server PowerShell Overview

TechNet Magazine PowerShell Topic (start with the oldest articles and work your way up)

PASS PowerShell Virtual Chapter

Allen White's Blog PowerShell Tag - A SQL Server community blogger that dables in PowerShell

Aaron Nelson's Blog PowerShell Tag - Another community blogger using PowerShell

Windows PowerShell Team Blog

PowerShell.com - has great e-books free to download and the Power Tips are great way to pick up new things (you can subscribe to them by email)

Don Jones' PowerShell Blog

Don Jones' Admin Crash Course to Windows PowerShell (FREE e-book)

MidnightDBA's PowerShell videos (Click on the PowerShell tab) Sean McCown has some great videos to get you introduced to PowerShell and using it with SQL Server. These videos kicked started me into wanting to learn more about PowerShell.


3.67 (45)

You rated this post out of 5. Change rating




3.67 (45)

You rated this post out of 5. Change rating