Sometimes you need to know the Windows Version of all domain controllers in the forest. This short snippet can help to find all domain controllers in the entire forest.

<# 
    Get all Domain Controllers Windows Versions
#>

# Set variables
$ADDomainController = @()
$Domains = (Get-ADForest).Domains

foreach($Domain in $Domains) {
    # Get some values from domain
    $ADDomain = Get-ADDomain -Identity $Domain
    $ADDcOU = $ADDomain.DomainControllersContainer

    # Get the fastest domain controller
    $DC = Test-Connection -ComputerName $ADDomain.ReplicaDirectoryServers -Count 1 | 
        Sort-Object -Property ResponseTime | 
        Select-Object -First 1 -ExpandProperty Address

    # Get all domain controler from this domain
    $ADDomainController += Get-ADComputer -Filter * -SearchBase $ADDcOU -Properties OperatingSystem -Server $DC |
        Select-Object Name,OperatingSystem

}

$ADDomainController | Sort-Object -Property OperatingSystem
$ADDomainController | 
    Group-Object -Property OperatingSystem | 
    Select-Object -Property Count, Name

Have fun

Leave a Reply

Your email address will not be published. Required fields are marked *