Tuesday, April 24, 2007

PowerShell and I just took our first steps towards what looks like a beautiful friendship.

I've been called a "Tools Guy". Not my words. I’m pretty sure it wasn’t a conspiracy either.

My best guess is that because I am fairly lazy, as I believe all good software developers should strive to be, and am not in the business of making work more difficult for myself; I'm always be looking for ways to off-load my efforts to the computer. That's why I check out different IDEs and databases and Nth generation languages to make it easier for me to do fun stuff with computer, and the less-fun stuff for the people who pay me. Long ago I learned that tools can help me to continue to be lazy. But to truly reap the benefit's of long term laziness I have to put in a little bit of work up front. So I invest time in playing with as many software tools as possible hoping to find a gem that can. Since I share this information with my fellow developers I guess I picked up the reputation along the way. It’s a bit ironic that ones laziness can lead to getting called anything other than unemployed.

Why would a lazy sod like me think that something named PowerShell could be friends? PowerShell sounds so dynamic and important. I mean, the tool has to be important, they didn’t put the word ‘power’ in there by accident, I think.

It all started back when PowerShell had the nonsense name Monad. That doesn’t even mean anything; much less give you a sense of authority. Maybe that’s it, like giving a bouncer a name like ‘Tiny’. Any road, I downloaded Monad and was immediately overwhelmed with a bunch of non-fun things that people were paying me to do, so I ignored it. Beside that it had a stupid name.

Several months later Microsoft officially released Monad with a much more important sounding name and I continued to ignore it, but the blogs were buzzing. Although I download and install it regardless, my packrat side is in constant battle with my lazy side.

Then today, I just need to do something very, very tiny. I need to quickly add dates together for a schedule I am planning. Normally I would just crack open Excel and throw a simple SUM() method into the first field, get my value, close Excel and move on.

=SUM(NOW(),90)

But that was too boring; I have a tool that I hadn’t played with. That situation needed to be rectified. I cracked open PowerShell, and having read someplace that it uses .NET as it’s underlying API I typed the C# call for the AddDays method.

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

PS X:\Home\mhall> System.DateTime.Now.AddDays(90)
The term 'System.DateTime.Now.AddDays' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:28
+ System.DateTime.Now.AddDays( <<<< 90)
PS X:\Home\mhall>

Hmm, that wasn't my desired result. Fortunately, it looks cool, and I was in a curious mood so I investigated. By 'investigated' I Google'd for the answer.

The Wonders of Date Math using Windows Powershell

That looked promising.

Apparently, PowerShell has a very different syntax that looks something like C# beat up Ruby and took its lunch money. But that’s okay; Ruby is been getting a bit uppity lately. Now we are cooking with PowerShell.

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

PS X:\Home\mhall> [System.DateTime]::Now.AddDays(90)

Monday, July 23, 2007 8:24:48 PM


PS X:\Home\mhall>

With a little bit of new syntax and a new tool I knocked 3 steps out of what was already a simple process. I love tools that make things easier.

One more tool in my belt, but this one will take me a while before I feel comfortable telling anybody about. No good talking about tools that you haven’t fully mastered.

Uhm, yeah. Nevermind.

Submit this story to DotNetKicks

2comments:

jsnover said...

Laziness == the mother of invention.

I like it and I concur. This is something my wife calls "creative laziness".

Good stuff.

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

MDH said...

Wow, my first comment ever. And from a tech celebrity too. I'm gushing.