Join an array with newline in PowerShell Join an array with newline in PowerShell powershell powershell

Join an array with newline in PowerShell


Pipe the array to the Out-String cmdlet to convert them from a collection of string objects to a single string:

PS> $body = $invalid_hosts -join "`r`n" | Out-String


It is sufficient just pipe to Out-String (see https://stackoverflow.com/a/21322311/52277)

 $result = 'This', 'Is', 'a', 'cat' $strResult = $result  | Out-String Write-Host $strResultThisIsacat


I'm unsure about how to answer everything else, but for guaranteed newlines in Powershell, use:[Environment]::NewLine in place of your "`n"