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 -match 'Configuration.*Platform.*(Debug|Release)' }
      | %{
        if (-not $_.TreatWarningsAsErrors) {
            $node = $proj.CreateElement('TreatWarningsAsErrors', $proj.Project.NamespaceURI)
            $node.InnerText = 'true'
            $node = $_.AppendChild($node)
            $modified = 1
    if ($modified -eq 1) {
This entry was posted in PowerShell, Programming. Bookmark the permalink.