2

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.

arco444
  • 22,002
  • 12
  • 63
  • 67
user2682459
  • 999
  • 2
  • 13
  • 24
  • Check [Remove lines from text file if it contains string Powershell post](http://stackoverflow.com/questions/24326207/remove-lines-from-text-file-if-it-contains-string-powershell), [Loop through files in a directory using PowerShell](http://stackoverflow.com/questions/18847145/loop-through-files-in-a-directory-using-powershell), and please try yourself first. – Wiktor Stribiżew Jul 17 '15 at 11:01

1 Answers1

3

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
}
arco444
  • 22,002
  • 12
  • 63
  • 67