Remove Multi-Line String from File with PowerShell

by gretty at 2013-04-30 19:13:10


I am attempting to remove a string from a file using Powershell. The string is from another file.

My code can successfully read the file but not remove the string from the other file. Whats going wrong and how can I fix it?

$inputFile = ".\usermenuTest1.4d"
$outputFile = ".\usermenuTest2.4d"
$destinationFile = "$"

# Read input file into variable
$target = [IO.File]::ReadAllText($inputFile)

# Replace target in output file
(Get-Content $outputFile) | Foreach-Object {
$_ -replace $target, ‘’
} | Set-Content $destinationFile
by mjolinor at 2013-04-30 19:23:00
$Target is going to be a multi-line string.


(Get-Content $outputFile) | Foreach-Object {
$_ -replace $target, ''
} | Set-Content $destinationFile

is going to go through each line in $outputfile, one at a time, and try to match them to that multi-line string. There’s no way that match is going to happen.