Error checking in PowerShell

I recently thought I ought to lift my scripting game and introduce some error checking into my PowerShell scripts.

After a quick google I found this excellent article from the Scripting Guys.

In essence, we set:

$error.clear()
$erroractionpreference = “SilentlyContinue”

And then use, the following statement to test whether the last command was successful:

if (!$?)
{
“An error has occurred.”
}

If you just want to know whether an error occurred within the script, you can test:

if ($error[0])
{
“An error has occurred”
}

The article is good and well worth a read. Watch this space for some examples shortly ….

Windows Script 5.7 Released!

I didn’t see this one coming.

Microsoft have released 5.7 of Windows Script. This provides an update for:

Microsoft® Windows® Script containing Visual Basic® Script Edition (VBScript.) Version 5.7, JScript® Version 5.7, Windows Script Components, Windows Script Host 5.7, and Windows Script Runtime Version 5.7.

Get your downloads here:
Windows 2000
Windows XP
Windows Server 2003

Utilising CMD behaviour in PowerShell

A couple of days ago Jeffrey Snover posted this article on the Windows PowerShell Team blog. I had noted this behaviour previously and just put it down to a quirk of PowerShell.

So I’ve been musing over what I miss from the CMD that I’d like available in PoSh. To help jog my memory I fired up a CMD session and typed

help > help.txt

That’s quite a list!
CMD Help output in notepad
I started to take a look at what command I might be interested in outside of the few Jeffrey highlighted. At first glance I just though about writing a loop to check if an alias already existed, and then create a function for them all. Looking at the list though and some of these are not suitable for PoSh anymore.

So other than dir, assoc, and vol is there anything to note? Well I did find that a few of the commands appear to be supported natively.
PowerShell CMD’s
Well I agree with Mark Russinovich. I like to be able to call the dir command with switches sometimes, but admittedly I’ve been weaned off its use since using PoSH full time.

I think the only other command I would be tempted to map over would be ftype. It’s a compliment to assoc, and could have an occasional use.

Ftype command in PowerShell
Notice that there deliberately appears to be no open action associated with the Microsoft.PowerShellScript.1 ProgId…