2
votes

For every file in a directory I wish to remove lines that match a regular expression (beginning with |B for example) using powershell.

I think I can do this via Get-ChildItem on the directory, foreach-object, get-content and some sort of if -match but I'm really struggling to fit it all together.

Any help would be massively appreciated. This is the first time I've ever written a powershell script.

1

1 Answers

3
votes

Something like the below should get you in the right direction

$files = Get-ChildItem "C:\your\dir"

foreach ($file in $files) {
  $c = Get-Content $file.fullname | where { $_ -notmatch "^\|B" }
  $c | Set-Content $file.fullname
}