Get the OperatingSystem and Service Pack

To find the OS version on the local machine:
PS C:\> [Environment]::OSVersion
or
PS C:\> [Environment]::OSVersion.Version

To get the OS version of a remote machine, you could run this same command with Invoke-Command

PS C:\> $ver = Invoke-Command -Computername computer64 -ScriptBlock {[Environment]::OSVersion.Version}
PS C:\> $ver

However this is quite slow and requires setting up PS Remoting so a faster / easier alternative is to use WMI/CIM:
PS C:\> Get-CimInstance Win32_OperatingSystem -computer 'computer64' | select Name, version, servicepackmajorversion, BuildNumber, CSName, OSArchitecture, OperatingSystemSKU

This command can then be wrapped into a script which accepts a computername and returns the OS/version:

Get-OSVersion.ps1

function Get-OSVersion {
    <#
    .Synopsis
    Get the operating system Version

    .Description
    The function gets the version of the operating system on
    a local or remote computer.

    .Parameter ComputerName
    Enter the name of a local or remote computer. The default
    is the local computer ("localhost").

    .Parameter LogFile
    Enter the name of a file
    
    .Notes
    Get-OSVersion reads the OS properties from the Win32_OperatingSystem WMI/CIM class.

    .LINK
    https://ss64.com/ps/syntax-osversion.html

    .Outputs
    Displays several strings with different system information.
    The ComputerName and Service pack can optionally be output to a log file.

    .Example
    Get-OSVersion

    .Example
    get-osVersion Server64 

    .Example
    # Get all computers with a name ending with db
    $comps = get-adcomputer -filter {name -like "*db"} | select name
    
    # Then pipe the list of computer names to get-osversion
    $comps.name | get-osversion
    
    #>
[cmdletbinding()]
Param (
    [parameter(ValueFromPipeline=$True)]
    $ComputerName="localhost",
    
    $Logfile
)

PROCESS {
    write-host "[$ComputerName]"

    if( (Test-Connection $ComputerName -Quiet -count 1)) {
        $osinfo = Get-CimInstance Win32_OperatingSystem -computer $ComputerName | select Name, version, servicepackmajorversion, BuildNumber, CSName, OSArchitecture, OperatingSystemSKU

        $os = $osinfo.Name     # OS Name
        $servicepack = $osinfo.servicepackmajorversion  # Service pack level
        $bitness = $osinfo.OSArchitecture  # 32 or 64 bit
        $build = $osinfo.BuildNumber       # Build Number
        $machineName = $osinfo.CSName      # Name of the machine
        $edition = $osinfo.OperatingSystemSKU  # Windows Edition (home, enterprise etc)

        " Build: $build   Bitness: $bitness   Edition: $edition"
        " ServicePack: $servicepack   OS: $os"
        if ($Logfile) {"[$ComputerName] ServicePack: $servicepack OS: $os" | out-file -append -filepath $Logfile}
        
    }
    else {
        Write-Error "$ComputerName Not Responding"
    }

}  # End of PROCESS block / next ComputerName
}

The function can be added to an existing script or psprofile, or module or just pasted into the current session.
You can then run the function with:
GetOSVersion.ps1 computer64

“The hero has died as a modern man; but as eternal man - perfected, unspecific, universal man, - he has been reborn” ~ Joseph Campbell

Related:

Get-CimInstance - Get WMI class information
VER - Display version information
WQL (WMI Query Language) - msdn.microsoft.com


Copyright © SS64.com 1999-2018
Some rights reserved