Remove Multi-Line String from File with PowerShell

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #5870

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

    Hello

    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 = "$outputFile.new"

    # 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.

    This:

    (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.

You must be logged in to reply to this topic.