Servers in Domain

At some point, I had a desire to list all the computer accounts for any server OS in Active Directory.  I am pretty sure that I did a search and found the script below, but I don’t remember where, so whoever wrote it doesn’t get credit this time…

$strCategory = “computer”
$strOperatingSystem = “Windows*Server*”

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain

$objSearcher.Filter = (“OperatingSystem=$strOperatingSystem”)

$colProplist = “name”
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()
Write-Host $colResults.count
foreach ($objResult in $colResults)
    {
    $objComputer = $objResult.Properties;
    $objComputer.name
    }

If you change the $strOperatingSystem = “Windows*Server*” to something like $strOperatingSystem = “Windows*”  it will return all computer accounts that have “Windows” in the Name field on the Operating System tab of the properties of the AD object:

image

Leave a Reply