Posted On May 19, 2020

PowerShell: Get Windows OS Name

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Get Windows OS Name
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)

Leave a Reply

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

Related Post

PowerShell: Demote Domain Controllers

$computername=$env:computername $localAdminPassword='PASSWORD' function demoteDc($computername=$env:computername,$localAdminPassword){ $erroractionpreference=stop $encryptedPass=convertto-securestring $localAdminPassword -asplaintext -force Import-Module ActiveDirectory $thisComputer=$env:computername $fsmoRoles=Get-ADDomainController -Filter *|Select-Object…

How To Stop, Start, Restart a Windows Service Being Stuck in ‘Stopping’ Status

Convenient Function:(related to https://blog.kimconnect.com/powershell-kill-a-windows-service-forcefully) # forceRestartService.ps1 # version 0.0.2 $serviceName='Spooler' function forceRestartService($serviceName='Spooler'){ $processId=Get-WmiObject -Class Win32_Service…

PowerShell: Microsoft Clustered Disks Creation Script v 0.10

Code: <# Microsoft_Clustered_Disks_Creation_V0.10.ps1Purpose:This snippet streamlines the process of creating physical disk volumes on a Windows…
Other project: DragonCoin.com