# getUsersAndComputersInActiveDirectory.ps1

$ouName="Test OU"
$ouPath = "ou=$ouName,dc=kimconnect,dc=yoyo"
$csvExportFile = 'c:\data\users_in_ou_$ouName.csv'

$users=Get-ADUser -Filter * -SearchBase $ouPath | Select-object Name,UserPrincipalName,DistinguishedName
#$computers=get-adcomputer -filter * -searchbase $ouPath | select-object Name,@{Name='Container';Expression={DistinguishedName}}
$computers=get-adcomputer -filter * -searchbase $ouPath | select-object Name,DistinguishedName
get-wmiobject win32_operatingsystem -cn $ouName

# Export results as a CSV file
$users|Export-Csv -NoType $csvExportFile
# List Windows Machines by Operating System Versions Inside a Specific OU
$windowsComputers=(Get-ADComputer -searchbase $ouPath -properties * -filter "OperatingSystem  -like 'Windows*' ").name

function convertWindowsBuildToName($number){
    $osName=switch ($number){
            18363{'Windows 10 (1909)'}
            18362{'Windows 10 (1903)'}
            17763{'Windows 10 (1809)'}
            17134{'Windows 10 (1803)'}
            16299{'Windows 10 (1709)'}
            15063{'Windows 10 (1703)'}
            14393{'Windows 10 (1607)'}
            10586{'Windows 10 (1511)'}
            10240{'Windows 10'}
            9600{'Windows 8.1'}
            9200{'Windows 8'}
            7601{'Windows 7 SP1'}
            7600{'Windows 7'}
            6002{'Windows Vista SP2'}
            6001{'Windows Vista SP1'}
            6000{'Windows Vista'}
            3790{'Windows XP3'}
            2600{'Windows XP2'}
            2195{'Windows XP'}
            default{'Unknown'}
            }
    return $osName
}

$osVersions=Get-ADComputer -Filter "OperatingSystem -like 'Windows*'" -Properties OperatingSystemVersion | group OperatingSystemVersion | select Name,Count
$sortedOsVersions=$osVersions|Sort-Object @{e={ [int](.{[void]($_.Name -match "\((\d+)\)");return $matches[1]}) }}
$sortedOsVersions|%{$_.Name = .{            
             $number=[int](.{[void]($_.Name -match "\((\d+)\)");return $matches[1]});
             convertWindowsBuildToName $number
             }
             }
$sortedOsVersions

<# Sample Output
Name              Count
----              -----
Windows XP           99
Windows XP2          99
Windows XP3          99
Windows Vista SP2     9
Windows 7            99
Windows 7 SP1      9999
Windows 8            99
Windows 8.1         999
Windows 10            9
Windows 10 (1511)    99
Windows 10 (1607)    99
Windows 10 (1703)    99
Windows 10 (1709)    99
Windows 10 (1803)    99
Windows 10 (1809)   999
Windows 10 (1903)   999
Windows 10 (1909)  9999
#>