Windows Command Line has got smarter

Most die-hard command-line fans tend to agree that the Windows Command Prompt isn’t great. When you consider how old it is, it isn’t all that powerful. This isn’t the end of the world, as there is always cygwin.

But now Microsoft have released their new command prompt PowerShell. I tried downloading it this evening, and so far, I really like it.

I heard of PowerShell last year when Ars Technica published a walkthrough on it while it was still in beta. But I hadn’t got round to giving it a try. Actually, despite it’s age, the Ars Technica guide is still the best introduction I’ve found, so if you want to know more about PowerShell is all about, it’s worth a look.

Some of my favourite features so far:
Access the Registry in the same way as files

PS C:\\>
PS C:\\> Get-PSDrive

Name       Provider      Root                                   CurrentLocation
----       --------      ----                                   ---------------
Alias      Alias
C          FileSystem    C:\\
cert       Certificate   \\
Env        Environment
Function   Function
HKCU       Registry      HKEY_CURRENT_USER
HKLM       Registry      HKEY_LOCAL_MACHINE
Variable   Variable
W          FileSystem    W:\\
X          FileSystem    X:\\
Y          FileSystem    Y:\\
Z          FileSystem    Z:\\

PS C:\\> cd HKLM:
PS HKLM:\\> cd SOFTWARE\\IBM\\MQSeries\\CurrentVersion\\Configuration\\
PS HKLM:\\SOFTWARE\\IBM\\MQSeries\\CurrentVersion\\Configuration> dir

   Hive: Microsoft.PowerShell.Core\\Registry::HKEY_LOCAL_MACHINE\\SOFTWARE\\IBM\\MQ

SKC  VC Name                           Property
---  -- ----                           --------
  0   1 ACPI                           {DoDialog}
  0   1 AlertMonitor                   {Startup}
  0   1 AllQueueManagers               {DefaultPrefix}
  0   1 ClientExitPath                 {ExitsDefaultPath}
  0   4 DefaultConfiguration           {V2_MachineName, V2_MQQMName, V2_Mach...
  0   6 LogDefaults                    {LogDefaultPath, LogPrimaryFiles, Log...
  3   0 QueueManager                   {}
  4   0 Services                       {}
  0   0 Trace                          {}
  0   1 WebAdministration              {Trace}

You can also access system certificates, environment variables, and more in the same way. Very cool!
SQL-type syntax
No more fighting with awk to get bits that I want out of command output! Instead of returning strings, PowerShell is object-oriented, and you can run a variety of commands with the returned objects.

For example…

PS C:\\MyFolder\\src> Get-ChildItem | select name


PS C:\\MyFolder\\src> Get-ChildItem | select name, extension

Name                                    Extension
----                                    ---------
amqssl.h                                .h
amqsslchk.c                             .c
amqsslchk.exe                           .exe
amqsslcon.c                             .c
amqsslenv.c                             .c
amqsslerr.c                             .c
amqsslgsk.c                             .c
amqsslikm.c                             .c
amqsslpcf.c                             .c
amqsslper.c                             .c
amqsslutl.c                             .c
amqsslwmq.c                             .c
compile.bat                             .bat
compile_with_ikeyman.bat                .bat                        .sh                        .sh

PS C:\\MyFolder\\src> Get-ChildItem | select name, extension | sort-object extension

Name                                    Extension
----                                    ---------
compile_with_ikeyman.bat                .bat
compile.bat                             .bat
amqsslper.c                             .c
amqsslpcf.c                             .c
amqsslwmq.c                             .c
amqsslutl.c                             .c
amqsslenv.c                             .c
amqsslcon.c                             .c
amqsslchk.c                             .c
amqsslikm.c                             .c
amqsslgsk.c                             .c
amqsslerr.c                             .c
amqsslchk.exe                           .exe
amqssl.h                                .h                        .sh                        .sh

There is lots more you can do, but if you’re familiar with the sort of FOR, WHILE, SELECT and so on in SQL, then you’ll be in familiar territory. It’s really nice.
You can use .NET API
There are lots of nice bits of functionality in .NET, and now you can access them interactively at the command-line. For example:

PS C:\\> ([xml](new-object Net.WebClient).DownloadString("
uk/blog/?feed=rss2")) | select title

I can be a C-numptie
Maintaining two blogs is difficult
The differences between men and women
winter has arrived
Using Scheduled Tasks and batch files to do automatic backups
Young people make a massive contribution to society
What IET members do in the evenings
The etch-a-sketch approach to legislation

You can use pretty much any function in a .NET library, so even GUIs can be created on-the-fly at the command-line:

PS C:\\> [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\\WINDOWS\\assembly\\GAC_MSIL\\System.Windows.Forms\\

PS C:\\> $window = new-object Windows.Forms.Form
PS C:\\> $window.text = "This is a Windows form!"
PS C:\\> $button = new-object Windows.Forms.Button
PS C:\\> $button.text = "Click me!"
PS C:\\> $window.controls.add($button)
PS C:\\> $window.showDialog()

and up pops a window with a button on it!

I’ve still got more to learn, but I’ve already started to knock together some very powerful scripts. If you’ve ever found the Windows Command Prompt a bit limited, I’d recommend you download it and have a play.

Other PowerShell resources that I found useful are tagged on my bookmarks

5 Responses to “Windows Command Line has got smarter”

  1. andyp says:

    This is very cool, I’ve been following the development of PowerShell for a while.

    The thing that put me off from installing it was the ominous “back up your entire system before proceeding” message!

    The syntax looks a bit scary, but I hope to get used to it, once I can bring myself to install.

  2. Pete says:

    This does look very powerful. The one thing I would pick out from your examples is that commands look very verbose. I’m the first one to demand long, descriptive variable and method names in actual code, but on the commandline where almost everything is a throwaway one-liner I would rather have less to type. It looks like “Get-ChildItem” is the Powershell equivalent of “ls”, and I know which one I’d rather type 100 times a day. Hopefully the existing DOS commands work too, so for this example I could type “dir” if I wanted. But I’m sure there are many cases where this does not apply.

    Overall though, kudos to Microsoft for coming up with something like this.

  3. […] mentioned PowerShell – the new Windows command shell and scripting language – last year when I first tried it out. But […]

  4. […] I won’t go into the background about PowerShell – if you’re interested, a quick search will turn up masses of information, everything from detailed guides to the first impressions of obscure bloggers! […]

  5. Dewani Anil says:

    This helped me in one of my projects. You rock!