Eric Bergman-Terrell's Blog

PowerShell RTMs, and I've Written My Last .BAT Command File
November 17, 2006

I developed on Unix systems before I became a Windows software developer Although Unix wasn't exactly user-friendly in those days (the late 1980's), Unix included powerful scripting languages and shells, and powerful commands that could be called from scripts. Compared to the Unix Bourne shell, Korn shell, CSH, or BASH (my favorite), the MS-DOS .BAT command language was an under-powered joke.

When I beta tested first version of Windows NT, I was astonished and disappointed to find that the command shell language for Windows NT was essentially the MS-DOS .BAT command language. If Microsoft had shipped NT with a more powerful shell, I think more Unix users and developers would have moved to the NT platform when it came out.

Microsoft began addressing this "scripting gap" by developing a command shell codenamed "Monad" for the "Longhorn" operating system. I've seen presentations on Monad and it looks like a winner. "Monad" has been named "PowerShell" and it RTM'ed (released to manufacturing) a day or two ago. PowerShell is built into Vista, but it's also compatible with Windows XP SP 2. You can download it here. Note, PowerShell requires the .NET 2.0 framework.

The Unix command shells allowed the textual output of one command to be used as the input for another command. For example, if memory serves, the foloowing command lists all processes (ps -a). The output from that command, a textual list of running processes, is used as input for the grep program, which in turn outputs all lines of its input that contain the text "csh". Using one command's output as the input of another command is called "piping".

ps -a | grep csh

PowerShell extends the Unix piping concept to the object world. PowerShell commands output objects, not text. Consequently it's not necessary to parse a command's output to extract specific data. For example, consider the following:

PS C:\temp> get-date

Friday, November 17, 2006 8:29:28 AM

PS C:\temp> get-date | format-list -property *


DisplayHint : DateTime
DateTime    : Friday, November 17, 2006 8:22:47 AM
Date        : 11/17/2006 12:00:00 AM
Day         : 17
DayOfWeek   : Friday
DayOfYear   : 321
Hour        : 8
Kind        : Local
Millisecond : 796
Minute      : 22
Month       : 11
Second      : 47
Ticks       : 632993485677968750
TimeOfDay   : 08:22:47.7968750
Year        : 2006

The get-date command displays the current date and time. But the get-date command really outputs an object, not lines of text. When get-date is piped to "format-list -property *", the object's properties are displayed. From the above display it's clear that the object output by get-date has a Millisecond property.

To extract the Millisecond property of the current date, no parsing is required:

PS C:\temp> get-date | format-list -property millisecond


Millisecond : 656

You can extract metadata about object members with get-member:

PS C:\temp> date | get-member millisecond


   TypeName: System.DateTime

Name        MemberType Definition
----        ---------- ----------
Millisecond Property   System.Int32 Millisecond {get;}

Now that Microsoft has released a powerful scripting environment, with hooks into the .NET platform, that doesn't entail elaborate text parsing, I can confidently state that I've written my last .BAT command script!

Keywords: PowerShell, command shell, .NET, scripts, scripting

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015