function getOsBuild($computername=$env:computername){
$build=""
$pingable=test-connection $computername -Count 1 -Quiet
if($pingable){
$psinfoIsAvailable=get-command psinfo -ea SilentlyContinue # Requires port 445
if($psinfoIsAvailable){$build=invoke-expression "psinfo \\$computername|%{if(`$_ -match 'Kernel build number:\s+(\d+)'){`$matches[1]}}" -ea SilentlyContinue}
else{
$winRmIsAvailable=Test-WSMan -ComputerName $computername -ea SilentlyContinue # Requires port 5985 5986
if($winRmIsAvailable){$build=invoke-command -computername $computername -scriptblock{[System.Environment]::OSVersion.Version.Build} -ea SilentlyContinue}
}
}
if ($build){
$osName=switch ($build){
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
}
else{
return $false
}
}
# Usage Example:
$targetOs="Windows"
$targetMachines=(Get-ADComputer -Filter "OperatingSystem -like '$targetOs'" -Properties OperatingSystem).DNSHostName
$targetMachines|?{getOsBuild $_}
# Deprecated version
function getOsName($computername=$env:computername){
try{
$build=invoke-expression "psinfo \\$computername|%{if(`$_ -match 'Kernel build number:\s+(\d+)'){`$matches[1]}}" -ea Continue
if (!$build){$build=invoke-command -computername $computername -scriptblock{[System.Environment]::OSVersion.Version.Build}}
}
catch{
throw $Error
}
if ($build){
$osName=switch ($build){
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'}
2600{'Windows XP2'}
}
return $osName
}
else{
return $false
}
}
PS C:\Windows\system32> getOsName Windows 10 (1809)
Categories: