As answered by @TobyU, using the -Raw
will easily resolve the issue but there is a downside to this quick solution:
PowerShell is extremely good in streaming objects, that's why Get-Content
supplies a stream of lines in the first place. If you use the -Raw
and/or brackets, you choke the output pipeline. This is not a big issue for a small list but could cause performance issues and/or memory issues when the list gets larger.
In respect of the PowerShell streaming pipeline you might consider to resolve it like this:
$On = $True; Get-Content -Path test.txt | Where {If ($_ -eq '#start') {$On = $False} ElseIf ($_ -eq '#end') {$On = $True} Else {$On}}
In this command, Where
filters the #Start
and #End
as it has in both cases no output at all and passes the rest of the lines when $on
is $True
.