I have the following powershell script which replaces multiple lines of text inside any text file and it works well, but I have a minor beef with it. Whenever it replaces the lines I want it adds a blank line at the end of the txt file.
The code:
$oldCode = @"
<uservalue1 value="0" />
<uservalue1 value="1" />
"@
$newCode = @"
<uservalue1 value="1" />
<uservalue1 value="2" />
"@
ls myfile.txt | foreach {
$fileContent = Get-Content $_.FullName -Raw
$newFileContent = $fileContent -replace $oldCode, $newCode
Set-Content -Path $_.FullName -Value $newFileContent
}
This only changes two values but it will also add an extra line break and therefore when this script runs multiple times a day then we end up with a large file containing blank lines at the end.
Is there a way to prevent this?