Powershell to recursively strip C# regions from files

Here’s a super quick little powershell snippet to strip regions out of all C# files in a directory tree. Useful for legacy code where people hide long blocks in regions rather than encapsulate it into smaller methods/objects.

dir -recurse -filter *.cs $src | foreach ($_) {
    $file = $_.fullname
    echo $file
    (get-content $file) | where {$_ -notmatch "^.*\#(end)?region.*$" } | out-file $file
}

Run this in your solution folder and support the movement against C# regions!

7 thoughts on “Powershell to recursively strip C# regions from files

  1. Pingback: DotNetShoutout
  2. Thanks Rich. Found an easier way if some people in your team don’t want them all removed.

    In VS:
    Tools -> Options -> Text Editor -> C# -> Advanced -> Uncheck “Enter outlining mode when files open”

    Now I don’t care if they are there :)

Comments are closed.