Content Redirection

Here’s another item I see in some submisstions of the 2010 Scripting Games that I felt I should address: the use of legacy console redirection. While technically not illegal or wrong, an example like this demonstrates (at least in my opinion) that the scripter hasn’t fully adopted the PowerShell paradigm.

While this will work and create a text file, it is not fully written in the PowerShell spirit. At the very least, you should use the cmdlets Out-File, Add-Content or Set-Content in place of the legacy console redirection operators, > and >>.   I’ll admit that 99 times out of 100 you’ll have no issues with the resulting text file.  However, there is the potential for encoding issues. The cmdlets are better designed to handle encoding as well as a few other PowerShell bells and whistles like –noclobber.

Another reason I suggest using cmdlets is that the > and >> operators are actually part of the underlying command session that PowerShell.exe is running on. It’s conceivable that some future version of PowerShell or some PowerShell hosted application won’t support these operators which means your script will fail and need revision. Stick with cmdlets from the beginning to protect your scripting investment.

Here’s a better PowerShell implementation that uses cmdlets and takes better advantage of the pipeline.

In this example I’m using Tee-Object which not only displays the properties in the console but also redirects to the text file. I’m sure you’ll agree this is better than using > and >>.  Or am I making too much out of this?

Summary Judgment

While working on a new article for REDMOND magazine about PowerShell 2.0, I wanted to get some cmdlet information. I wanted an easy way to see a list of cmdlets for a given verb or noun. Of course that is easily done with Get-Command. However this only gives my the definition.

After a little experimenting I found that I could get the help synopsis with the actual Get-Help cmdlet, not the Help function. So combining the two gives me the results I need.

PS C:\> gcm -noun job | select Name,@{Name="Synopsis";Expression={(get-help $_).synopsis}} | ft -auto -wrap

Name        Synopsis
—-        ——–
Get-Job     Gets Windows PowerShell background jobs that are running
            in the current session.
Receive-Job Gets the results of the Windows PowerShell background jobs
            in the current session.
Remove-Job  Deletes a Windows PowerShell background job.
Start-Job   Starts a Windows PowerShell background job.
Stop-Job    Stops a Windows PowerShell background job.
Wait-Job    Suppresses the command prompt until one or all of the
            Windows PowerShell background jobs running in the
            session are complete.

This should work for most Get-Command expressions. Although you may need to tweak a bit. Here’s my solution for discovering about aliases:

PS C:\> gcm -type alias | select Name,ResolvedCommandName,@{Name="Synopsis";Expression={(get-help $_).synopsis}} | ft -wrap -auto

Have fun.