This script connects to Active Directory OU or just the root of the domain and recursivly gets all the computer objects  underneath.
for each AD computer object it gets the Opertating system Type , build number and Service pack using information of the AD object and not using WMI Queries.
so this is really usefull if you want to gather this kind of information even if the computers are turned off

PowerShell
Edit|Remove
# ==============================================================================================
# 
#
# NAME: ADCompOSinfo 
# 
# AUTHOR: Mohamed Garrana , 
# DATE  : 3/25/2010
# 
# COMMENT: 
# This script connects to Active Directory OU or just the root of the domain and recursivly gets all the computer objects  
# for each AD computer object it gets the Opertating system Type , Version and Service pack using information of the AD object
# ==============================================================================================

function get-alladcomputers {

	Trap { Write-Host -ForegroundColor Red "   <<< Error >>>  could not connect to Active directory, please make sure that ADpath var is correctly SET !"
		Break
	
	}
	
	$ADpath = "LDAP://DC=mantrac,DC=win2k,DC=dom"
	$searcher = New-Object DirectoryServices.DirectorySearcher
	$RootSearch = New-Object directoryservices.directoryentry $ADpath
	$searcher.searchroot = $RootSearch
	$searcher.filter = "(objectClass=computer)"
	$allcomps = $searcher.findall() 
	foreach ($comp in $allcomps) { get-adcomputerinfo }	
	$objs = $allcomps | Measure-Object
	$objcount = $objs.Count
	Write-Host -BackgroundColor green -ForegroundColor blue " Script succesfully Completed on $objcount Active Directory computer objects"
	}

function get-adcomputerinfo {
	BEGIN { }
	PROCESS  {
	Write-Host $comp.Properties.name
	try {
	$name = $comp.Properties.name[0] 
	$os= $comp.Properties.operatingsystem[0] 
	$osver= $comp.Properties.operatingsystemversion[0] 
	$ossp=$comp.Properties.operatingsystemservicepack[0]
	}
	Catch {
	Write-Host -ForegroundColor Red "   <<< WHoops ... >>>  $name : Error reading a required property from the AD computer object, this might be a cluster resource name or the property has a null value . execution will continue anyway ;)"
		continue
		}
	finally {

	$computer = New-Object psobject 
	$Computer | Add-Member NoteProperty Name ($name)
	$computer | Add-Member NoteProperty OperatingSystem ($os)
	$Computer | Add-Member NoteProperty OSVersion ($osver)
	$computer | Add-Member Noteproperty OSServicePack ($ossp)
	Write-Output $computer
	}
	} 
	END{}
}
#set your variables :- 


#setting the ADpath variable
$ADpath = "LDAP://DC=microsoft,DC=com" # this will grap all the AD computer objects in the microsoft.com domain
#$ADpath = "LDAP://OU=Marketing,DC=Microsoft,DC=dom" #this will grap all the AD computer objects in the marketing OU in the microsoft domain

#you can use the script like this to export the output into a csv file with all the computers and their OS information
$outfile = "c:\test\ADcomps2.csv" #sets the location of the out csv file if you'r going to use Export-csv
get-alladcomputers | Export-Csv $outfile

#you can use the script like this to see a nice grid view of all the computers and their OS information
get-alladcomputers | Out-GridView