PowerShell Community Extensions

I started the PowerShell Community Extensions (PSCX) project to fill in  some of the gaps in the set of built-in Windows PowerShell commands.  Over the years, some of the PSCX command equivalents have made their way into Windows PowerShell including Start-Process, Select-Xml, Get-WebService, and Get-Random.  However, for anyone who has used the standard set of UNIX utilities via packages such as cygwin or MKS Toolkit knows, there are still quite a few missing commands in Windows PowerShell.  Here are some of my favorites that PSCX provides.

One of the first cmdlets I missed was a PowerShell equivalent to the octal dump (od) utility.  In PowerShell terms, we would call this a formatter as in Format-Hex:

Another PSCX formatter I use a lot is the Format-Xml cmdlet.  This cmdlet is great for viewing pesky single-line, 8000 character wide XML files:

As a developer, I deal with lots of XML files (app.config files, MSBuild and TeamBuild project files, etc).  One cmdlet I find very handy to use on these XML files before checking them in is Test-Xml:

The Test-Xml cmdlet verifies that the XML is well-formed and if a schema is provided it will also validate the XML against the schema.

I’ve contributed a number of PowerShell scripts to our build and test processes.  Those PowerShell scripts can break our build if I’m not careful when I’m checking in updates.  I use the Test-Script cmdlet to make sure a script is free of syntax errors before checking it in:

Note: the Test-Script cmdlet uses the PSParser tokenizer provided in PowerShell v2 which only catches syntax errors and not runtime errors.

If you’ve ever needed to execute a batch file to modify environment variables for the current PowerShell session, then Invoke-BatchFile will come in very handy.  Normally with the execution of a batch file, the new environment variable definitions exist only in the spawned cmd.exe process.  Invoke-BatchFile will import those environment variable definitions back into the PowerShell session that executed the batch file.  I use this command to import Visual Studio environment variables into my PowerShell session:

For developers, the Test-Assembly cmdlet can be useful in your build scripts.  For example, when you need to re-sign partially signed assemblies you don’t want to apply the re-signing utility to native DLLs:

One of the commands that can be useful to any PowerSheller is a native application called echoargs.exe. You use it when you’re trying to troubleshoot problems passing arguments to native applications.  You stand a much better chance of fixing the problem if you can see what PowerShell is passing to the native application.  echoargs.exe is used as a stand-in for your application.  All it does is showing you the command line arguments that PowerShell passes to the application.  This Team Foundation command fails when executed in PoweShell: tf.exe status . /r /workspace:*;hillr.  If you substitute echoargs for the .exe file, then you can see what is going on:

The term hillr is not recognized as the name of a cmdlet.

echoargs shows that the ;hillr part of the argument doesn’t even make it to the application.  The problem is that the “;” character is a statement separator in PowerShell.  You need to put quotes around the argument to ensure it gets to tf.exe in one piece:

There are many more useful PSCX commands such as Set-FileTime, Set-Writable, Set-ReadOnly, Unblock-File, and Show-Tree.  Hopefully I’ve piqued your interest in the PowerShell Community Extensions.  If so, give PSCX a try at http://pscx.codeplex.com.

About the author: Keith Hill

Keith Hill has been a software developer on Windows since 1992 using C, Objective C, C++ and C#. I am a veteran of many Microsoft PDC’s with eight under my belt (93,96,97,00,03, 05, 08, 10 and BUILD 2011). At the 2003 PDC, I sat in on session about the future of Windows administration and automation. That was the first public peek at Monad and I’ve been hooked on PowerShell ever since.

Related Posts

%d bloggers like this: