Script to turn on "Treat Warnings as Errors"

This PowerShell script recursively finds all .csproj files in a directory and sets “Treat warnings as errors” to “All” for every one of them that doesn’t already have it set. I definitely prefer this compilation option, but on larger systems getting those all flipped can be a tedious process.

get-childitem . -include *.csproj -recurse | %{
    [xml]$proj = get-content $_.FullName
    $modified = 0
      | where-object { $_.Condition <span class="preproc">-match</span> <span class="str">'Configuration.*Platform.*(Debug|Release)'</span> }
      | %{
        <span class="kwrd">if</span> (-not $_.TreatWarningsAsErrors) {
            $node = $proj.CreateElement(<span class="str">'TreatWarningsAsErrors'</span>, $proj.Project.NamespaceURI)
            $node.InnerText = <span class="str">'true'</span>
            $node = $_.AppendChild($node)
            $modified = 1
    <span class="kwrd">if</span> ($modified <span class="preproc">-eq</span> 1) {
This entry was posted in PowerShell, Programming. Bookmark the permalink.