Munging Strings in Windows PowerShell

Bean thread is often made of mung beans, but in Windows PowerShell, the capabilities to manipulate character string expressions can draw both on the power of .NET and the expression syntax particular to PowerShell.

This brief article will certainly not delve into the seemingly infinite possibilities of string manipulation nor any comparison to languages such as LISP or Icon. Instead, let’s just look at a bit of Exchange Server trivia expressed in PowerShell.

If you are an administrator of Exchange Server 2007 or have looked at the upcoming Exchange Server 2010, you know that version numbers expressed about Exchange aren’t always aligned with the “under the hood” version numbers. Because of the alignment with Office 2007 (Office 12), Exchange Server 2007 (RTM = 8.0) has often been referred to as Exchange 12. If you’re not an Exchange administrator, just read along for fun – we’ll get to string manipulation after the trivia story.

If you’ve transitioned from the world of Exchange Server 2003 to Exchange Server 2007 you may know that old features such as the ability to have multiple Administrative Groups and multiple Routing Groups were replaced with other ways of doing delegations of administrative settings and usually relying on Active Directory sites for internal message routing. But what about backward compatibility? Well, Exchange Server 2007 and Exchange Server 2010 environments have one administrative group in which all newer Exchange Servers reside. Similarly, E2K7 and E2010 have one routing group for backward compatibility as well.

That’s the background trivia. Now for a bit of PowerShell.

new-object string (,(“EXCHANGE12ROCKS”.ToCharArray() | %{ [char]([int]($_)+1) }))

And the output of this little code fragment is “FYDIGOHF23SPDLT” which is part of the name of the backward-compatible administrative group Exchange Administrative Group (FYDIGOHF23SPDLT).

new-object string (,(“EXCHANGE12ROCKS”.ToCharArray() | %{ [char]([int]($_)-1) }))

A similar code fragment, with a minus sign instead of a plus sign, decrements each letter in the original string rather than incrementing each as the previous example had. This results in the string “DWBG@MFD01QNBJR” which is not exactly part of the backward-compatible routing group name Exchange Routing Group (DWBGZMFD01QNBJR), in that the actual translation used by Microsoft wrapped the “decrement of A” back around the alphabet to “Z” rather than the to the previous Unicode/Latin-1/ASCII character “@” which our little code snippet generated. There are several ways our code snippet could be “fixed” to yield the proper result, yet those shall not be investigated here and now.

Sure, there are easier ways to explain strange names used on Active Directory objects, and there are likely easier ways to translate strings in PowerShell. The point however is this – PowerShell possesses a versatile expressivity with its ability to take a pipeline expression and use it as a parameter to a cmdlet or function.

-Brad Werner

Related Course:

Automating Windows Server 2008 Administration with Windows Powershell

In this article

Join the Conversation