{"id":73921,"date":"2015-08-17T00:01:00","date_gmt":"2015-08-17T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/08\/17\/use-the-powershell-5-convert-string-cmdlet\/"},"modified":"2019-02-18T09:35:38","modified_gmt":"2019-02-18T16:35:38","slug":"use-the-powershell-5-convert-string-cmdlet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-powershell-5-convert-string-cmdlet\/","title":{"rendered":"Use the PowerShell 5 Convert-String Cmdlet"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about using the new <\/span><b style=\"font-size:12px\">Convert-String<\/b><span style=\"font-size:12px\"> cmdlet in Windows PowerShell 5.0 to manipulate strings.<\/span><\/p>\n<p>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&mdash;to be honest, I think he was bumming scones, but as it turned out he was out of luck&mdash;I ate all the scones I had.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8-17-15-1.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8-17-15-1.jpg\" alt=\"Photo of bird\" title=\"Photo of bird\" \/><\/a><\/p>\n<p>Anyway, exploring is lots of fun and an interesting way to spend the day.<\/p>\n<p>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.<\/p>\n<h2>Convert-String<\/h2>\n<p>One of the stealth cmdlets making an appearance in Windows PowerShell 5.0 is the <b>Convert-String<\/b> cmdlet. It is possible you have heard about <b>ConvertFrom-String<\/b>, which is also a new cmdlet&mdash;but more than likely, <b>Convert-String<\/b> has remained under the covers. In fact, when I first was reading through the documentation for Windows PowerShell 5.0, I saw <b>ConvertFrom-String<\/b>, and then in the very next sentence, I saw <b>Convert-String<\/b>. I thought I had discovered a documentation bug.<\/p>\n<p>Not so. <b>Convert-String<\/b> 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.<\/p>\n<p>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.<\/p>\n<h2>The old flipty dipty<\/h2>\n<p>A very common task among IT operations is manipulating user&#039;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.<\/p>\n<p>Like I said, not horribly difficult if you had done that a lot&mdash;but still pretty hard to write from scratch the first time around.<\/p>\n<p>With <b>Convert-String<\/b>, I don&rsquo;t even need to write a script to do this. I can do it from the command line. Here is an example:<\/p>\n<p style=\"margin-left:30px\">&quot;Mu Han&quot;, &quot;Jim Hance&quot;, &quot;David Ahs&quot;, &quot;Kim Akers&quot; | Convert-String -Example &quot;Ed Wilson=Wilson, E.&quot;<\/p>\n<p>The way <b>Convert-String<\/b> 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&nbsp;10:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-17-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-17-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Expanding the string idea<\/h2>\n<p>I know that I don&rsquo;t have to manually type in strings to get the <b>Convert-String<\/b> 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.<\/p>\n<p>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 <b>$a<\/b> variable. The <b>$a<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">$a = gps -Name svchost | select processname, id | ConvertTo-Csv -NoTypeInformation | select -Last 13<\/p>\n<p style=\"margin-left:30px\">$a | Convert-String -Example &#039;&quot;svchost&quot;, &quot;219&quot;=219, s.&#039;<\/p>\n<p>When I run the code, I obtain the output shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-17-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-17-15-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using the <b>Convert-String<\/b> cmdlet. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[609,3,21,608,45],"class_list":["post-73921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-powershell-5","tag-scripting-guy","tag-string-manipulation","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=73921"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/73921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=73921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=73921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=73921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}