James Kovacs

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Getting Started with PowerShell - Developer Edition

PowerShell Unix-style Recently I was on the PowerScripting Podcast hosted by Hal Rottenberg and Jonathan Walz. I had a great time talking about PowerShell from a developer’s perspective and psake, my PowerShell-based build system, in particular. You can find the interview on Episode 56 here. Thanks to Hal and Jonathan for having me on the show.

Now let’s talk PowerShell and scripting for developers. I don’t see a lot of developers using the command line and this surprises me. Maybe it’s my Unix background that attracts me to the command line. Maybe it’s my belief that sustainable, maintainable development is facilitated by a CI process, which necessitates being familiar with the command line. Maybe it’s because the only way to create reproducible results is to automate and the easiest way to automate is the command line. Whatever the reason, I believe that developers should become familiar with copying, building, deploying, and otherwise automating tasks via the command line.

So why PowerShell? PowerShell is a full-fledged programming language focused on object-based shell scripting. Note that I didn’t say “object-oriented”. You cannot create class hierarchies or define polymorphic relationships, but you can instantiate and use objects defined in other .NET-based programming languages (or COM objects). PowerShell is a shell language, meaning that it serves the same role as cmd.exe, bash, tcsh, etc. It’s raison d’etre is automating the command prompt. Manipulating directories/files, launching applications, and managing processes is really straightforward.

Microsoft is investing heavily in PowerShell and providing support for managing Windows Server 2008, IIS7, SQL Server 2008, Exchange 2007, and other Microsoft server products. This means that we are approaching the Nirvana that is end-to-end build to deploy. Imagine getting latest from your source repository, building the code, unit/integration testing the code, building the documentation, labelling the build, and deploying into a QA environment! The entire process is automated and reproducible. You know the exact version of the code that went into QA. When you get the green light to do a live deployment, you give the same PowerShell scripts to the IT deployment team who uses it to deploy the compiled (and QA’d) bits into the production environment! Who best to write these deployment scripts than your friendly neighbourhood IT Pro, who is intimately familiar with the production and QA environments. This is a great collaboration point to get the IT Pros, who will be deploying and maintaining your apps, involved in the development process.

Enough chitchat. Show me the code! Believe it or not, you probably already know a fair amount of PowerShell. Many of the commands you’re familiar with in cmd.exe work in PowerShell. (More surprisingly, many of the common commands you know from bash, tcsh, or other Unix shells also work!) The command line arguments are often different, but the basic familiar commands are there. So try out dir, cd, copy, del, move, pushd, popd, … (If you’re a old Unix hacker, you can try out ls, man, kill, pwd, ps, lp, cp, … Unfortunately there is no grep equivalent built in, which is terribly unfortunate.) All of these commands are actually aliases to PowerShell commands, which are named by VERB-NOUN, where NOUN is singular. For example to get a list of running processes, you run Get-Process, which is aliased to “ps”.

PowerShell is very conducive to experimentation. You can always find out more about a command or alias typing “Get-Help [CmdName|Alias]” or simply “help [CmdName|Alias]” since help is an alias for Get-Help. (N.B. PowerShell is case insensitive.) You can also look for commands by typing part of the command and pressing tab repeatedly. For example, if you want to find all set- commands, type “set-[TAB][TAB]…” to display Set-Acl, Set-Alias, etc. You can also look for commands using wildcards. Type “*-Acl[TAB][TAB]…” displays Get-Acl and Set-Acl.

So start playing around with PowerShell. Learn what it can do for you. Next time, we’ll look at writing re-usable scripts for accomplishing common developer tasks. Until then, happy scripting!


Posted Sun, Feb 1 2009 3:21 PM by james.kovacs
Filed under:

[Advertisement]

Comments

Chris Tavares wrote re: Getting Started with PowerShell - Developer Edition
on Sun, Feb 1 2009 6:54 PM

There is a GREP equivalent - select-string

Morten wrote re: Getting Started with PowerShell - Developer Edition
on Sun, Feb 1 2009 7:27 PM

The problem with PowerShell is, that many functions like in Exchange 2007 now only is available through PowerShell and not by calling an API. This is good if you like to run your administration from the command line, but what if you want to do it through managed code? This can be done yes, but it is not very efficient.

Powershell is in my opnion not what all systems administrators want as you cannot put these scripts in the hands of someone who has a little knowledge and frankly using a command prompt  this is a step back in time. I do not know what drowe Microsoft in this direction, internal Unix freaks?, but they surely forgot to ask the people who are using their systems.

static_readonly wrote re: Getting Started with PowerShell - Developer Edition
on Mon, Feb 2 2009 8:26 AM

Really looking forward to the rest of this series.

Christopher Bennage wrote re: Getting Started with PowerShell - Developer Edition
on Mon, Feb 2 2009 8:27 AM

@James I've been interested in learning more about PS for some time now. Please keep these coming!

Dew Drop - February 2, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop - February 2, 2009 | Alvin Ashcraft's Morning Dew
on Mon, Feb 2 2009 10:09 AM

Pingback from  Dew Drop - February 2, 2009 | Alvin Ashcraft's Morning Dew

DotNetShoutout wrote Getting Started with PowerShell - Developer Edition
on Mon, Feb 2 2009 10:23 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

james.kovacs wrote re: Getting Started with PowerShell - Developer Edition
on Tue, Feb 3 2009 12:28 AM

@Chris - I didn't know that (obviously). Definitely adding select-string to my repertoire!

@Morten - The reality is that although you can write a UI over a command line tool with relative ease (think VS driving MsBuild), writing a command line tool over a GUI tool is well near impossible. I'm sure you'll see visual tooling, either by Microsoft or a third-party, that will wrap the tasty PowerShell goodness in a comfortable GUI coating.

Weekly Link Post 80 « Rhonda Tipton’s WebLog wrote Weekly Link Post 80 « Rhonda Tipton’s WebLog
on Sun, Feb 8 2009 9:48 PM

Pingback from  Weekly Link Post 80 « Rhonda Tipton’s WebLog

Weekly Links #39 | GrantPalin.com wrote Weekly Links #39 | GrantPalin.com
on Mon, Feb 9 2009 10:11 AM

Pingback from  Weekly Links #39 | GrantPalin.com

Add a Comment

(required)  
(optional)
(required)  
Remember Me?