Learn Powershell, Discover .NET (Part 1)

I’ve been scripting on the windows platform for many years. Yet I only recently learnt to use Powershell. This was a steep learning curve for me but absolutely worth it. Knowledge workers might not immediately see the benefits of learning to script. In the following series I will try to outline what’s powerful about Powershell and why you should learn it.

I’m old enough and the industry moves fast enough that younger readers might not remember the windows script host and the VBScript and JScript engines that shipped with (and still do) with Windows. I still use a VBScript that sends a numlock keystroke every three minutes because our admins don’t trust me to lock a workstation.

Windows OS exposed the file system and a number of other systems as COM objects that could be instantiated by the scripting engines. There was regular expressions and dictionary objects and there are many things we can accomplish. The downside was there was a lot of overhead to doing anything useful with VBScript and JScript, and script kiddies like me learnt to keep snippets of functions and subs handy because nobody wants to write things twice (or even once).

So just how painful was VBScript to use? Consider the following code snippet to read the contents of a file.

Set FSO = CreateObject("Scripting.FileSystemObject")
Set file = FSO.OpenTextFile("C:\test.txt", 1)
content = file.ReadAll

First we need an instance to the Scripting.FileSystemObject, then we need to open the text file for reading. Finally we can assign the result from the ReadAll method to our variable.

Now let’s compare the equivalent in Powershell.

$content = Get-Content C:\test.txt

A single line of code.

The Powershell version is easy to remember and also makes sense compared to what we had to do in VBScript. And we are still only scratching at the surface of how Powershell makes the windows operating system actually useful.

Another thing that sets the shell above the old windows scripting host is that we are now interactive. VBScripts were difficult to debug. Users would typically edit the script in notepad and execute by double clicking in explorer. The code is compiled at runtime, which means you can get compile errors or runtime errors. We would resort to writing MsgBox calls as a debugging tool.

Powershell is interactive. This makes it very easy to write and debug scripts, since you can test the effect (and syntax) of a statement immediately. And when a script stops you can preserve the state and check the values of the variables.

The last thing I will say about Powershell for now is that it is a gateway for learning .NET. It’s hard to understate the relationship between the two Microsoft technologies. Powershell is, at its core, an interactive shell to the .NET framework. Using it you can instantiate and manipulate just about any .NET object. It also allows or the objects to be passed around as collections to be processed by other cmdlets through a pipeline. That might sound a bit cryptic to users unfamiliar with unix shells, but for those who are the Powershell pipeline is considerably more advanced. We are not just passing lines of text but actual handles to the objects. Because of this it’s possible to write highly functional programs with minimal lines of code.

This will be a three part series (or maybe four) on Powershell with a very brief introduction to .NET (from a user perspective). In Part 2 we will look at the history of the shell and in Part 3 we will look at Powershell in a little more depth.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.