title: Powershell - Filtering OUs while using get-adcomputer
body: <p>I am trying to create a script that generates a list of computers based on specific properties which a computer may have. For example, I am trying to make a list of Windows XP computers and Windows 7 computers, throw their names in a .csv file and outputting the final count of each.</p>
  
  <p>Here is my code so far</p>
  
  <pre><code>import-module ActiveDirectory
  $computers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem"
  $i = 0
  $j = 0
  Foreach ($computer in $computers) {
      if ($computer.operatingSystem -like "Windows 7*") {
          $i++
          '"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
          }
      elseif ($computer.OperatingSystem -like "Windows XP*") {
          $j++
          '"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
          }
      else {
          $_
          }
  
  }
  write-host "$i Win 7"
  write-host "$j Win xp"
  $k = $i+$j
  write-host "$k Total"
  </code></pre>
  
  <p>Sample Output:</p>
  
  <pre><code>104 Win 7
  86 Win xp
  190 Total
  </code></pre>
  
  <p>This script works however I would like to make it a bit better by being able to say which OU's not to look into, but I can't quite figure it out.</p>
  
  <p>If anyone has any insight into how to do this, or even just to make the above code any better I would love to hear it.</p>
  
  <p>Thank you!</p>