Converting String Array in String

by Jun 5, 2013

When you use Get-Content to read the content of a text file, you always get back a string array. So each line of your text file is kept separately.

If you want to convert a string array into one large string, use the operator -join:

$file = "C:\Windows\WindowsUpdate.log"

$text = (Get-Content -Path $file -ReadCount 0) -join "`n"

In PowerShell 3.0, you can do the same with the new parameter -Raw:
$text = Get-Content -Path $file -Raw

Twitter This Tip! ReTweet this Tip!