Scan by OU?

Oct 22, 2014 at 6:52 PM
Edited Oct 22, 2014 at 6:52 PM
Hi there,
First off, THANKS for this, just the thing I was looking for (and was going to attempt to develop)...but is there a way to scan by OU in AD? Or the reason I ask is I only want to scan servers in our network and exclude workstations with SQL services it possible to do that (or filter out those ones)?
We have an OU for just servers which would be handy to use if possible.
Nov 11, 2014 at 4:41 PM
Good suggestion! Unfortunately there's nothing baked in to scan by OU but the nice thing about PowerShell is that there are ways to get that with minimal effort (and they're probably better written, too!)

One way would be to use Get-ADComputer to search for an OU in your domain. Check out for documentation; here's a basic example of how you'd use it:
Import-Module -Name ActiveDirectory

$ADComputer = Get-ADComputer -Filter * -SearchBase 'ou=Kendal,dc=demo,dc=local' |
    Where-Object {
        -not [String]::IsNullOrEmpty($_.DNSHostName)
    } |
    Select-Object -ExpandProperty DNSHostName

$ComputerName = $ADComputer -join ','

Remove-Module -Name ActiveDirectory
Another possibility would be to use System.DirectoryServices objects, which are outlined (including plenty of great examples) at