Joining Lines with PowerShell

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 the content looks like this:
[sourcecode language=”powershell”]
Cmcdlet,Action
Get-ExOJob,Added
Set-ExOJobAdded
Remove-ExOJob,Added
[/sourcecode]
For 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:
[sourcecode language=”powershell”]
$Line = 'Skype Online Cmdlet Changes: '
$Temp = @($Line
$TempTweet += @($Change2)
$Temp > Temp.txt
$Message = (Get-Content 'Temp.txt') -join " "
$NewMessage = $Message.Replace(',',' was ')
[/sourcecode]
In 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 ‘-join ” ” ‘. 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:
[sourcecode language=”powershell”]
Cmcdlet,Action Get-ExOJob,Added Set-ExOJobAdded Remove-ExOJob,Added
[/sourcecode]
No 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:

Contents of First Variable

Number of lines changed from 778 to 774.
Number of files did not change in number.

Contents of Second Variable Line Changes:

Lines Added:

Lorem ipsum dolor sit amet.
Vulputate enim nulla aliquet porttitor lacus luctus.
Turpis egestas pretium aenean pharetra magna.
Massa sapien faucibus et molestie ac feugiat.

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?
[sourcecode language=”powershell”]
$Body = (Get-Content $ChangeLineFile) -join '<br />'
$Body2 = (Get-Content $ChangeFile) -join '<br />'
$Body = $Body + '<br />'+'<br />' +$Body2
[/sourcecode]
After the third line, the $Body Variable looks like this:

Number of lines changed from 778 to 774
Number of files did not change in number.

Lines Added: Lorem ipsum dolor sit amet.
Vulputate enim nulla aliquet porttitor lacus luctus.
Turpis egestas pretium aenean pharetra magna.
Massa sapien faucibus et molestie ac feugiat.

Now that the $Body variable has both variables content, we can now add it to an email and send it out:
[sourcecode language=”powershell”]
Send-MailMessage -to $To -from $From -subject $subject -bodyashtml -body $body -smtpserver $SMTPServer
[/sourcecode]
Why 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.

Related Post