I want to add content into the middle of a text file in Powershell. I'm searching for a specific pattern, then adding the content after it. Note this is in the middle of the file.
What I have currently is:
(Get-Content ( $fileName )) |
Foreach-Object {
if($_ -match "pattern")
{
#Add Lines after the selected pattern
$_ += "`nText To Add"
}
}
} | Set-Content( $fileName )
However, this doesn't work. I'm assuming because $_ is immutable, or because the += operator doesn't modify it correctly?
What's the way to append text to $_ that will be reflected in the following Set-Content call?