Skip to main content

Demonstrates how to split lines into an array in PowerShell. Handles both unix and windows line endings.

$content = @'


line three
line four


line seven
line eight


'@

$lines = $content -split '\r?\n|\r' | Where-Object {$_.Trim() -ne ''}

Write-Output -InputObject $lines
# line three
# line four
# line seven
# line eight

# NOTE: The top and bottom two lines are removed, however, the two empty
# lines between (between four and seven) are not.

$lines.GetType()
# IsPublic IsSerial Name                                     BaseType
# -------- -------- ----                                     --------
# True     True     Object[]                                 System.Array