title: How do I insert text after a specific line in files using powershell?
body: <p>As part of a huge refactoring I removed some duplicate classes and enums. I moved namespaces and restructured everything to be easier to maintain in the future.</p>
  
  <p>All changes has been scripted already except for one thing. I need to insert a data contract namespace in every file that uses another namespace if the data contract namespace has not been inserted yet.</p>
  
  <p>The code I have at the moment does not work but is sort of what I need I guess.</p>
  
  <pre><code>function Insert-Usings{
      trap {
          Write-Host ("ERROR: " + $_) -ForegroundColor Red
          return $false 
      }
      (Get-ChildItem $base_dir -Include *.asmx,*.ascx,*.cs,*.aspx -Force -Recurse -ErrorAction:SilentlyContinue) | % {
      $fileName  = $_.FullName
      (Get-Content $fileName) | 
          Foreach-Object 
          {
              $_
              if ($_ -cmatch "using Company.Shared;") { 
                      $_ -creplace "using Company.Shared;", "using Company.Common;"
              }
              elseif ($_ -cmatch "using Company") {
                  #Add Lines after the selected pattern 
                  "using Company.Services.Contracts;"
              }
              else{
                  $_
              }
          }
      } | Set-Content $fileName
  }
  </code></pre>
  
  <p><strong>Edit:</strong> The code tends to output (overwrite the whole file with-) "using Company.Services.Contracts" statements.</p>