How to get Operatingsystem from all domain controllers in the forest

Written by Arne Tiedemann on Tuesday October 23, 2018

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