{"id":40,"date":"2019-09-17T19:58:42","date_gmt":"2019-09-17T19:58:42","guid":{"rendered":"https:\/\/www.practicalpowershell.com\/post\/joining-lines-with-powershell"},"modified":"2020-03-06T19:53:56","modified_gmt":"2020-03-06T19:53:56","slug":"joining-lines-with-powershell","status":"publish","type":"post","link":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/","title":{"rendered":"Joining Lines with PowerShell"},"content":{"rendered":"<p>The ability to join multiple lines or multiple variables into one line or one variable can prove quite useful when working with certain data sets. <\/p>\n<p>Example #1<\/p>\n<p>For this example, a script compares two different files and exports the changes to a txt file for auditing purposes.  This file is read into a variable and the content looks like this:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nCmcdlet,Action<br \/>\nGet-ExOJob,Added<br \/>\nSet-ExOJobAdded<br \/>\nRemove-ExOJob,Added<br \/>\n[\/sourcecode]<br \/>\nFor this one the content is stored in a way that it cannot simply be joined. Instead, variable content is exported to a file, then imported and joined:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$Line = &#039;Skype Online Cmdlet Changes: &#039;<br \/>\n$Temp = @($Line<br \/>\n$TempTweet += @($Change2)<br \/>\n$Temp &gt; Temp.txt<br \/>\n$Message = (Get-Content &#039;Temp.txt&#039;) -join &quot; &quot;<br \/>\n$NewMessage = $Message.Replace(&#039;,&#039;,&#039; was &#039;)<br \/>\n[\/sourcecode]<br \/>\nIn the above code section the variable content is exported to a txt file. Then the txt file is read back in and each line is joined with &#8216;-join &#8221; &#8221; &#8216;. Now all of the lines are converted into one line, which in this case, can be used to tweet a message.  All of the lines would have been combined into one, like so:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nCmcdlet,Action   Get-ExOJob,Added    Set-ExOJobAdded    Remove-ExOJob,Added<br \/>\n[\/sourcecode]<br \/>\nNo longer a group of lines, but all content on one line. Example #2 For this example we have two variables.  Both need to be put into one email in the body of the message.  The content for both of these are stored in separate variables. Like so: <\/p>\n<p>Contents of First Variable <\/p>\n<p><em>Number of lines changed from 778 to 774.<br \/>\nNumber of files did not change in number. <\/em><\/p>\n<p>Contents of Second Variable Line Changes:<\/p>\n<p>Lines Added:<\/p>\n<p><em>Lorem ipsum dolor sit amet.<br \/>\nVulputate enim nulla aliquet porttitor lacus luctus.<br \/>\nTurpis egestas pretium aenean pharetra magna.<br \/>\nMassa sapien faucibus et molestie ac feugiat. <\/em><\/p>\n<p>The question now is, how to store both of these variables into one variable so we can then use the one variable for an email body?<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$Body = (Get-Content $ChangeLineFile) -join &#039;&lt;br \/&gt;&#039;<br \/>\n$Body2 = (Get-Content $ChangeFile) -join &#039;&lt;br \/&gt;&#039;<br \/>\n$Body = $Body + &#039;&lt;br \/&gt;&#039;+&#039;&lt;br \/&gt;&#039; +$Body2<br \/>\n[\/sourcecode]<br \/>\nAfter the third line, the $Body Variable looks like this: <\/p>\n<p><em>  Number of lines changed from 778 to 774<br \/>\n  Number of files did not change in number. <\/p>\n<p>  Lines Added: Lorem ipsum dolor sit amet.<br \/>\n  Vulputate enim nulla aliquet porttitor lacus luctus.<br \/>\n  Turpis egestas pretium aenean pharetra magna.<br \/>\n  Massa sapien faucibus et molestie ac feugiat. <\/em><\/p>\n<p>Now that the $Body variable has both variables content, we can now add it to an email and send it out:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nSend-MailMessage -to $To -from $From -subject $subject -bodyashtml -body $body -smtpserver $SMTPServer<br \/>\n[\/sourcecode]<br \/>\nWhy would we use the -join function?  It comes in handy when data may not be lined up and need to join multiple rows into a single row. Joining could then be used to improve the format of the outputted data to a file or to PowerShell session even.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ability to join multiple lines or multiple variables into one line or one variable can prove quite useful when working with certain data sets. Example #1 For this example, a script compares two different files and exports the changes to a txt file for auditing purposes. This file is read into a variable and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":[],"rop_publish_now_history":[],"rop_publish_now_status":"pending","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Joining Lines with PowerShell - Practical PowerShell<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Joining Lines with PowerShell - Practical PowerShell\" \/>\n<meta property=\"og:description\" content=\"The ability to join multiple lines or multiple variables into one line or one variable can prove quite useful when working with certain data sets. Example #1 For this example, a script compares two different files and exports the changes to a txt file for auditing purposes. This file is read into a variable and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical PowerShell\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-17T19:58:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-06T19:53:56+00:00\" \/>\n<meta name=\"author\" content=\"damian\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"damian\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/\"},\"author\":{\"name\":\"damian\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"headline\":\"Joining Lines with PowerShell\",\"datePublished\":\"2019-09-17T19:58:42+00:00\",\"dateModified\":\"2020-03-06T19:53:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/\"},\"wordCount\":491,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/\",\"name\":\"Joining Lines with PowerShell - Practical PowerShell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\"},\"datePublished\":\"2019-09-17T19:58:42+00:00\",\"dateModified\":\"2020-03-06T19:53:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/joining-lines-with-powershell\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/practicalpowershell.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Joining Lines with PowerShell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/\",\"name\":\"Practical PowerShell\",\"description\":\"PowerShell books written by experts\",\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/practicalpowershell.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\",\"name\":\"damian\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"caption\":\"damian\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Joining Lines with PowerShell - Practical PowerShell","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/","og_locale":"en_US","og_type":"article","og_title":"Joining Lines with PowerShell - Practical PowerShell","og_description":"The ability to join multiple lines or multiple variables into one line or one variable can prove quite useful when working with certain data sets. Example #1 For this example, a script compares two different files and exports the changes to a txt file for auditing purposes. This file is read into a variable and [&hellip;]","og_url":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/","og_site_name":"Practical PowerShell","article_published_time":"2019-09-17T19:58:42+00:00","article_modified_time":"2020-03-06T19:53:56+00:00","author":"damian","twitter_card":"summary_large_image","twitter_misc":{"Written by":"damian","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/#article","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/"},"author":{"name":"damian","@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"headline":"Joining Lines with PowerShell","datePublished":"2019-09-17T19:58:42+00:00","dateModified":"2020-03-06T19:53:56+00:00","mainEntityOfPage":{"@id":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/"},"wordCount":491,"commentCount":0,"publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/","url":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/","name":"Joining Lines with PowerShell - Practical PowerShell","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/#website"},"datePublished":"2019-09-17T19:58:42+00:00","dateModified":"2020-03-06T19:53:56+00:00","breadcrumb":{"@id":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/practicalpowershell.com\/joining-lines-with-powershell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/practicalpowershell.com\/"},{"@type":"ListItem","position":2,"name":"Joining Lines with PowerShell"}]},{"@type":"WebSite","@id":"https:\/\/practicalpowershell.com\/#website","url":"https:\/\/practicalpowershell.com\/","name":"Practical PowerShell","description":"PowerShell books written by experts","publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/practicalpowershell.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb","name":"damian","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","caption":"damian"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/40\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}