{"id":1003,"date":"2014-07-15T00:01:00","date_gmt":"2014-07-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/15\/keep-your-hands-clean-use-powershell-to-glue-strings-together\/"},"modified":"2014-07-15T00:01:00","modified_gmt":"2014-07-15T00:01:00","slug":"keep-your-hands-clean-use-powershell-to-glue-strings-together","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/keep-your-hands-clean-use-powershell-to-glue-strings-together\/","title":{"rendered":"Keep Your Hands Clean: Use PowerShell to Glue Strings Together"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about several approaches for concatenating strings by using Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This weekend, I was speaking about blogging and technical writing at the South Carolina Writers Workshop in Rock Hill, South Carolina. One of the cool things is a couple of people drove from Atlanta and from Raleigh, North Carolina to attend my sessions. It was great to see them.<\/p>\n<p>If you hang out with The Scripting Wife on Facebook, you know that we also moved over the weekend. It is just across town, but still it is a major disruption. This morning I am sitting on the floor in our new apartment, with my Surface Pro&nbsp;2 propped up on a cardboard box full of books, and I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Chick_Korea\" target=\"_blank\">Chick Corea<\/a> on my Zune. I have my Zune plugged in to an old pair of powered &ldquo;computer speakers&rdquo; to make an impromptu stereo. Here is a picture of my current workstation:<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-15-14-1.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-15-14-1.jpg\" alt=\"Image of computer\" title=\"Image of computer\" \/><\/a><\/p>\n<p>I can&rsquo;t find my Bose noise-cancelling headphones, or I would just use those. The nice thing is that our new apartment has free WiFi in the common areas, and the signal is strong enough in our apartment, so I am all set.<\/p>\n<p>The cool thing about my Surface Pro&nbsp;2 is that it, in fact, has Windows PowerShell. So I can write my blog post for today without any issues.<\/p>\n<h2>Joining strings<\/h2>\n<p>When it comes to the simple task of joining strings, Windows PowerShell offers multiple ways to accomplish this task. I know it can get somewhat complicated, but when I stop to think about it, the simple task of joining strings can get <i>really<\/i> complicated. For example, the easiest way to join two strings is to concatenate them. But even that choice has multiple options.<\/p>\n<h2>Concatenating strings<\/h2>\n<p>Let&#039;s take a look at how to concatenate (join together) a couple of strings. To do this, I will assign the strings to two variables. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $s = &quot;This is a string&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $t = &quot;a really cool string&quot;<\/p>\n<p>In Windows PowerShell the concatenation operator is also the one that is used for adding stuff together. So it sort of makes sense. To concatenate the string, I just add them together. Here is the command and the result:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $s + $t<\/p>\n<p style=\"margin-left:30px\">This is a stringa really cool string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>WooHoo! It worked!<\/p>\n<p>But wait. One might say it &ldquo;messed up.&rdquo; Actually, I wanted it to show up like that. Honest. Really. OK, maybe not.<\/p>\n<p>What happens when concatenating two strings like that, is it glues them together. If I want a separator, I need to specify it. Here is my revision:<\/p>\n<p>I use a comma and a space for my separator, and the output looks great:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $s + &quot;, &quot; + $t<\/p>\n<p style=\"margin-left:30px\">This is a string, a really cool string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h2>Expanding strings<\/h2>\n<p>An easier way to concatenate the strings is to use double quotation marks because they automatically expand the contents of the variable. So, instead of multiple concatenation operators, all I need to do is put everything in a pair of double quotation marks. When I do that, I add my comma and my space inside the string. This can make it a bit difficult to read, but it works fine. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; &quot;$s, $t&quot;<\/p>\n<p style=\"margin-left:30px\">This is a string, a really cool string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h2>Substitution<\/h2>\n<p>Because the expanding string can become difficult to read, I often like to use substitution values and the format specifier. To do this, I create a string and parameters for place holders. My separator goes inside the string. Then I use the <b>&ndash;f<\/b> format operator and specify the values for <b>{0}<\/b> and <b>{1}<\/b>. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; &quot;{0}, {1}&quot; -f $s,$t<\/p>\n<p style=\"margin-left:30px\">This is a string, a really cool string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<h2>Use the Join operator<\/h2>\n<p>Windows PowerShell has a <b>Join<\/b> operator that will glue strings together. It is a little funky and it takes a bit of practice to use, but when you understand it, it works well. Here is the way I first tried to use the <b>Join<\/b> operator:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $s -join $t<\/p>\n<p style=\"margin-left:30px\">This is a string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>Needless to say, I was not impressed with the output. So I had to look up the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847757.aspx\" target=\"_blank\">about_Join<\/a> topic in my online Help.<\/p>\n<p>I learn that if I want to join the strings together, I need to put <b>&ndash;Join<\/b> at the beginning of the command as shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; -join $s, $t<\/p>\n<p style=\"margin-left:30px\">This is a string<\/p>\n<p style=\"margin-left:30px\">a really cool string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>Well OK, that worked. Sort of. But it does not look like what I had in mind when I wanted to join the two strings. It looks more like an array of strings, which I would pretty much have anyway. How about take number two:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; -join ($s, $t)<\/p>\n<p style=\"margin-left:30px\">This is a stringa really cool string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>This is what I was really expecting. It looks like I need to group the strings together, and then <b>&ndash;Join<\/b> will concatenate them. Groovy. But now I am back where I started. I need to figure out how to add a delimiter.<\/p>\n<p>That is pretty easy. The online Help tells me that by default, there is no delimiter. But if I want a delimiter, I need to put it as the second argument to the operator. So here I go for attempt number four:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; -join ($s, $t), &quot;, &quot;<\/p>\n<p style=\"margin-left:30px\">This is a stringa really cool string<\/p>\n<p style=\"margin-left:30px\">,<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>Dude, that did not work either. Bummer.<\/p>\n<p>I look at the Help topic a bit closer, and I discover that If I want to join a string with a specific delimiter, I put the string on the left side, and I put the delimiter on the right side. Attempt number five:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $s, $t -join &quot;, &quot;<\/p>\n<p style=\"margin-left:30px\">This is a string, a really cool string<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;<\/p>\n<p>And it works! Note that this time, I did not have to group my strings together&mdash;I was able to supply the strings as a simple array of strings.<\/p>\n<p>So, like I said, the <b>&ndash;Join<\/b> operator is a little funky, but it does not take too much playing around to get it working properly.<\/p>\n<p>That is all there is to joining strings. String Week will continue tomorrow when I will talk about using .NET Framework methods to join strings.<\/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: Microsoft Scripting Guy, Ed Wilson, talks about several approaches for concatenating strings by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. This weekend, I was speaking about blogging and technical writing at the South Carolina Writers Workshop in Rock Hill, South Carolina. One of the cool things is a couple of people [&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":[3,4,336,45],"class_list":["post-1003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-strings","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about several approaches for concatenating strings by using Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. This weekend, I was speaking about blogging and technical writing at the South Carolina Writers Workshop in Rock Hill, South Carolina. One of the cool things is a couple of people [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1003","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=1003"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1003\/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=1003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}