1

test.txt contents:

foo

[HKEY_USERS\S-1-5-18\Software\Microsoft]

bar

delete me!

[HKEY_other_key]

end-------------

Online regex matches the text to be removed correctly (starting from string delete until string [HKEY), but code written in PowerShell doesn't remove anything when I run it in PowerShell ISE:

$file = [System.IO.File]::ReadLines("test.txt")
$pattern = $("(?sm)^delete.*?(?=^\[HKEY)")
$file -replace $pattern, "" # returns original test.txt including line "delete me!" which should be removed

It seems to be a problem with ReadLines because when I use alternative Get-Content:

$file = Get-Content -Path test.txt -Raw

it removes the unwanted line correctly, but I don't want to use Get-Content.

1 Answer 1

4

[System.IO.File]::ReadAllLines(..) reads all lines of the file into a string array and you're using a multi-line regex pattern.

Get-Content -Raw same as [System.IO.File]::ReadAllText(..), reads all the text in the file into a string.

[System.IO.File]::ReadAllText("$pwd\test.txt") -replace "(?sm)^delete.*?(?=^\[HKEY)"

Results in:

foo

[HKEY_USERS\S-1-5-18\Software\Microsoft]

bar

[HKEY_other_key]

end-------------

In case you do need to read the file line-by-line due to, for example, high memory consumption, switch -File is an excellent built-in PowerShell alternative:

switch -Regex -File('test.txt') {
    '^delete' {        # if starts with `delete`
        $skip = $true  # set this var to `$true
        continue       # go to next line
    }
    '^\[HKEY' {        # if starts with `[HKEY`
        $skip = $false # set this var to `$false`
        $_             # output this line
        continue       # go to next line
    }
    { $skip } { continue } # if this var is `$true`, go next line
    Default   { $_ }       # if none of the previous conditions were met, ouput this line
}
Sign up to request clarification or add additional context in comments.

2 Comments

ReadAllText works but it has about the same memory consumption that Get-Content had and hijacks memory after script is finished as well. switch -Regex -File seems to consume a bit less RAM but is very slow on a file with 650 000 lines (42 MB).
@van_folmert how is this related to the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.