Assuming you don't have a sequence like this that extends over more than two adjacent lines:
[ab
cd
ef]
Then this should work:
$file = "c:\junk\test.txt"
$BufferedLine = $null
Get-Content $file |
ForEach-Object {
if ($_ -match "^[^[].*[^]]$") {
# line doesn't begin with "[" or end with "]"
$_ # -- line is okay, just return it
}
elseif ($_ -match "^\[.*\]$") {
# line begins with "[" and ends with "]"
$_ # -- line is okay, just return it
}
elseif ($_ -match "^\[.*[^]]$") { # line begins with "[" but doesn't end with "]"
$BufferedLine = $_ # remember line as beginning
}
elseif ($_ -match "^[^[].*\]$") {
# line doesn't begin with "[" but ends with "]"
if ($BufferedLine) { # AND there's a preceeding line awaiting closure
$BufferedLine += $_ # concatenate with contents of previous line
$BufferedLine # return completed line
$BufferedLine = $null # and forget the value
}
}
elseif ($_.length -eq 0){
$_
}
}
if ($BufferedLine) {
$BufferedLine # return the last line of necessary
}