Use the PowerShell 5 Convert-String Cmdlet
Summary: Ed Wilson, Microsoft Scripting Guy, talks about using the new Convert-String cmdlet in Windows PowerShell 5.0 to manipulate strings.
Microsoft Scripting Guy, Ed Wilson, is here. Over the weekend, the Scripting Wife and I headed out and about to explore central Florida. We found a really cool lake town that had an awesome tea shop. We also saw some pretty cool birds walking around. Here is one bird that was just hanging out—to be honest, I think he was bumming scones, but as it turned out he was out of luck—I ate all the scones I had.
Anyway, exploring is lots of fun and an interesting way to spend the day.
So is spending a day exploring Windows PowerShell 5.0 in Windows 10. I am continuously amazed with the things I am finding. In the past, I have always advocated that serious network administrators learn at least the basics of Regular Expressions. They are a powerful way of manipulating strings, and they are everywhere in Windows PowerShell (at least as an option). But in Windows PowerShell 5.0, that becomes a little bit less of a requirement.
One of the stealth cmdlets making an appearance in Windows PowerShell 5.0 is the Convert-String cmdlet. It is possible you have heard about ConvertFrom-String, which is also a new cmdlet—but more than likely, Convert-String has remained under the covers. In fact, when I first was reading through the documentation for Windows PowerShell 5.0, I saw ConvertFrom-String, and then in the very next sentence, I saw Convert-String. I thought I had discovered a documentation bug.
Not so. Convert-String is way cool, extremely powerful, and more than a little finicky. This is a cmdlet that demands to be played with and explored to see what it can do for you. You will want to spend half-a-day or more experimenting before you can get really comfortable with it.
But the payoff will permit you to do amazing things from the command line. In mere minutes, you can accomplish what would have taken days of writing custom string manipulation code. It is that good.
The old flipty dipty
A very common task among IT operations is manipulating user's names. Typically, this takes the form of taking first name/last name and switching them around to last name/first initial. In the past with Windows PowerShell, this was not a major pain (if one had written such code), but it was a bit complex. I mean, I had to split the name at something like a comma, and create an array of name elements, then move the array of name elements around, and then use a string method to select the first initial of the second element in the array.
Like I said, not horribly difficult if you had done that a lot—but still pretty hard to write from scratch the first time around.
With Convert-String, I don’t even need to write a script to do this. I can do it from the command line. Here is an example:
"Mu Han", "Jim Hance", "David Ahs", "Kim Akers" | Convert-String -Example "Ed Wilson=Wilson, E."
The way Convert-String works is that I can pipeline strings. Then I specify an example of what I want my output to look like. If input strings match up properly with my pattern, the output I specify will be created. Here is what my previous code looks like when I run it on Windows PowerShell 5.0 on my laptop running Windows 10:
Expanding the string idea
I know that I don’t have to manually type in strings to get the Convert-String cmdlet to work. I mean, that would be like so last century. But can I dynamically create my input on the fly? Well, the following example proves this. I do not really have a specific use case scenario for my example, but it will give you an idea of some of the things you might want to play with.
I collect a collection of processes, and I look only for Svchost processes. I then choose the last 13 entries in my CSV that I create on the fly, and store them back into the $a variable. The $a variable now contains SVCHOST and the PID. Then I grab the PID and the SVCHOST name so that I can manipulate my output to form a specific pattern. Here is the code:
$a = gps -Name svchost | select processname, id | ConvertTo-Csv -NoTypeInformation | select -Last 13
$a | Convert-String -Example '"svchost", "219"=219, s.'
When I run the code, I obtain the output shown here:
That is all there is to using the Convert-String cmdlet. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy