Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Tuesday, January 12, 2010

Query AD for Operating system with PowerShell.

The following script is a modification of the one written by The Scripting Guy: http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx. You will find a detailed explanation of the steps below at the link above.

It will also return the OS version to you. I put my modifications in green


$strCategory = "computer"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher

$objSearcher.SearchRoot = $objDomain

$objSearcher.Filter = ("(objectCategory=$strCategory)")

$colProplist = "name", “operatingsystem”

foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)

{$objComputer = $objResult.Properties

$objComputer.name

$objComputer.operatingsystem

Write-host “ “

}

1 comment:

Tim said...

Thanks for this excellent script!

I had one issue: my AD is very large (large sum of computer objects). I needed to include this entry in order to get a complete listing:

$objSearcher.PageSize = 3000