First Glance at PowerShell

July 21st, 2010 by Max Kuipers Leave a reply »

A couple days ago I had the surprisingly excellent opportunity to learn and use Windows PowerShell… What? Don’t look at me like that. I disapprove of Microsoft just as much as the next Linux fanboy, but seriously, this was cool. Just give me a chance to explain. I swear, I was forced into the situation – one of the projects I was working on required a simple script be written to rename files on a Windows server, but for various reasons, I couldn’t use Cygwin. After a brief panic attack caused by the realization that I would have to be separated from my beloved Bash, I looked into which scripting language would be best. After an exhaustive, comprehensive, and fully extensive 30-second Google search I found myself with a choice between Powershell and classic Batch… Naturally, I chose Powershell.

Most Windows 7 computers come with PowerShell and all the necessary dependencies already installed, so I never actually had to install it, but from what I’ve read the setup is fairly simple – you just sort of install it.  You also get a little IDE called Powershell ISE which is basically just your standard, 3-pane scripting IDE (with a command-line interpreter in one pane, the output in another, and a third pane for writing and saving scripts.)

I tried my standard approach of consulting forums and the MSDN library for a little while but it seemed most of the documentation I could find was either circular and self-referencing or written by some rabid Microsoft fanatic who was so hopelessly drunk on the M$ Kool-Aid that every other word they wrote was some special Microsoft technology or practice. After about a half hour of reading online documentation, all that I really learned was that commands in PowerShell are called “cmdlets.” It’s not clear what the distinction is between a cmdlet and your standard Bash command in Linux, but I guess I’ve seen more pathetic cries for attention in the Windows world than this.

Slightly dismayed at my lack of progress in learning much from my usual way, I considered the much suggested get-help cmdlet…

UGH Ok, I’m just going to stop right here, I can’t make it any further calling these things “cmdlets.” Call me stubborn, or close-minded, but this is just ridiculous. I’m calling them “commands.”

…Anyways, silly nomenclature aside, this get-help command is kind of like a “man” page to a Linux user, it displays the syntax, description, and examples for any command in PowerShell. The get-help command is in some ways even a little more clever than man pages. For instance, I needed to know how to create a new directory so first thing I tried was get-help mkdir. While mkdir is not a command in PowerShell, nor does it even have a directly analogous single-command counterpart in PowerShell, it recognized what I was asking for and brought me to the page for the New-Item command which has a -type directory flag you can use to make a new directory. With this command, I was able to learn anything I needed to learn to make my script happen.

There were, of course, a few more bumps in the road, but nothing too serious; string manipulation is distinctly different from most other languages, and there was some difficulty in getting the permissions (or execution-policy) just right so that I could actually run the script. All-in-all, it took me about four hours to get the environment set up, learn enough of the language to be productive, and finally write and test the script I needed. Despite the handicap that it is a Microsoft-specific product, Powershell sincerely surprised me by being a fairly easy-to-learn and useful scripting language. I really only got to use a very small part of the language, but nonetheless, I would recommend Powershell to anyone who needs to write a script in Windows but cannot use Cygwin.

Advertisement

5 comments

  1. Cyrus says:

    I just got a windows 7 computer at work, so I’ll have to remember this. I have a feeling it will probably be pretty useful at some point.

  2. Jim Chrystal says:

    You’ve made a giant leap forward into the not-so-distant past. Good job!

  3. Ian says:

    Right on! Very intriguing.

  4. Christine says:

    As another linux fanboy (or girl in this case) forced to use Windows for my work, this is pretty interesting. I’d be curious to hear more about the permissions obstacles you faced (and how you overcame them), as well as where the most useful documentation you found was.

  5. Max Kuipers says:

    Christine, the permissions issues were simply an unfamiliar security protocol. It’s called the “execution-policy” in Powershell. Naturally, the default was set to not let anything run, so I had to figure out how to allow it to run. Instead of just chmod u+x on a particular file, I had to restart the shell and run it as an administrator and type “Set-ExecutionPolicy RemoteSigned” which allows any script I write to run without question but requires signature on any script downloaded from the internet.

Leave a Reply