Skip to content

Instantly share code, notes, and snippets.

@asheroto
Last active October 30, 2025 01:13
Show Gist options
  • Save asheroto/cfa26dd00177a03c81635ea774406b2b to your computer and use it in GitHub Desktop.
Save asheroto/cfa26dd00177a03c81635ea774406b2b to your computer and use it in GitHub Desktop.
Get-OSInfo is a PowerShell function that retrieves key details about the Windows OS, including Release ID, Display Version, Name, Type (Workstation/Server), Numeric Version, Edition ID, Version (an object that includes major, minor, and build numbers), and Architecture (OS architecture, not processor architecture).

WindowsTerminal_x2M1vJFJ59

Get-OSInfo

Retrieves information about the operating system version and architecture.

Description

The Get-OSInfo function is a PowerShell script that queries both the Windows registry and the Win32_OperatingSystem class to gather comprehensive information about the operating system.

The function returns the following:

Property Description Example Output
Release ID The release ID of the operating system 2004
Display Version The display version of the operating system 20H2
Name The product name of the operating system Windows 10 Pro
Type Workstation or Server Workstation
Numeric Version The numeric version of the operating system 10
Edition ID The edition ID of the operating system Professional
Version Object including major, minor, and build numbers 10.0.19042.0
Architecture OS architecture (not processor architecture) x64

Examples

Example 1

Get-OSInfo

This example retrieves the OS version details of the current system and returns an object with properties as mentioned above.

Output from Windows 10 Pro

ReleaseId       : 2004
DisplayVersion  : 20H2
Name            : Windows 10 Pro
Type            : Workstation
NumericVersion  : 10
EditionId       : Professional
Version         : 10.0.19042.0
Architecture    : x64

Output from Server 2022 Datacenter

ReleaseId       : 2009
DisplayVersion  : 21H2
Name            : Microsoft Windows Server 2022 Datacenter
Type            : Server
NumericVersion  : 2022
EditionId       : Datacenter
Version         : 10.0.20348.0
Architecture    : x64

Example 2

(Get-OSInfo).Version.Major

This example retrieves the major version number of the operating system. The Get-OSInfo function returns an object with a Version property, which itself is an object containing Major, Minor, and Build properties. You can access these sub-properties using dot notation.

Output

10

Example 3

$osDetails = Get-OSInfo
Write-Output "OS Name: $($osDetails.Name)"
Write-Output "OS Type: $($osDetails.Type)"
Write-Output "OS Architecture: $($osDetails.Architecture)"

This example stores the result of Get-OSInfo in a variable and then accesses various properties to print details about the operating system.

Output

Name: Windows 10 Pro
Type: Workstation
Architecture: x64

Notes

  • The function uses both registry values and CIM instances to gather information.
  • It includes error handling to catch any issues that may arise during execution.
  • The architecture details are specifically related to the OS, not the processor, and are represented as x32, x64, arm, or arm64.
function Get-OSInfo {
<#
.SYNOPSIS
Retrieves detailed information about the operating system version and architecture.
.DESCRIPTION
This function queries both the Windows registry and the Win32_OperatingSystem class to gather comprehensive information about the operating system. It returns details such as the release ID, display version, name, type (Workstation/Server), numeric version, edition ID, version (object that includes major, minor, and build numbers), and architecture (OS architecture, not processor architecture).
.EXAMPLE
Get-OSInfo
This example retrieves the OS version details of the current system and returns an object with properties like ReleaseId, DisplayVersion, Name, Type, NumericVersion, EditionId, Version, and Architecture.
.EXAMPLE
(Get-OSInfo).Version.Major
This example retrieves the major version number of the operating system. The Get-OSInfo function returns an object with a Version property, which itself is an object containing Major, Minor, and Build properties. You can access these sub-properties using dot notation.
.EXAMPLE
$osDetails = Get-OSInfo
Write-Output "OS Name: $($osDetails.Name)"
Write-Output "OS Type: $($osDetails.Type)"
Write-Output "OS Architecture: $($osDetails.Architecture)"
This example stores the result of Get-OSInfo in a variable and then accesses various properties to print details about the operating system.
#>
[CmdletBinding()]
param ()
try {
# Get registry values
$registryValues = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$releaseIdValue = $registryValues.ReleaseId
$displayVersionValue = $registryValues.DisplayVersion
$nameValue = $registryValues.ProductName
$editionIdValue = $registryValues.EditionId
# Strip out "Server" from the $editionIdValue if it exists
$editionIdValue = $editionIdValue -replace "Server", ""
# Get OS details using Get-CimInstance because the registry key for Name is not always correct with Windows 11
$osDetails = Get-CimInstance -ClassName Win32_OperatingSystem
$nameValue = $osDetails.Caption
# Get architecture details of the OS (not the processor)
$architecture = $osDetails.OSArchitecture
# Normalize architecture
if ($architecture -match "(?i)32") {
$architecture = "x32"
} elseif ($architecture -match "(?i)64" -and $architecture -match "(?i)ARM") {
$architecture = "ARM64"
} elseif ($architecture -match "(?i)64") {
$architecture = "x64"
} else {
$architecture = "Unknown"
}
# Get OS version details (as version object)
$versionValue = [System.Environment]::OSVersion.Version
# Determine product type
# Reference: https://learn.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.producttype?view=powershellsdk-1.1.0
if ($osDetails.ProductType -eq 1) {
$typeValue = "Workstation"
} elseif ($osDetails.ProductType -eq 2 -or $osDetails.ProductType -eq 3) {
$typeValue = "Server"
} else {
$typeValue = "Unknown"
}
# Extract numerical value from Name
$numericVersion = ($nameValue -replace "[^\d]").Trim()
# Create and return custom object with the required properties
$result = [PSCustomObject]@{
ReleaseId = $releaseIdValue
DisplayVersion = $displayVersionValue
Name = $nameValue
Type = $typeValue
NumericVersion = $numericVersion
EditionId = $editionIdValue
Version = $versionValue
Architecture = $architecture
}
return $result
} catch {
Write-Error "Unable to get OS version details.`nError: $_"
exit 1
}
}
@tadghh
Copy link

tadghh commented Aug 7, 2024

thx thx!

@OldHarmony
Copy link

Yes i can confirm. the info on display is a little bit wrong

Screenshot 2025-03-28 110135

@asheroto
Copy link
Author

@OldHarmony That's what I needed, thanks! Can you try using the updated function and show the output?

@OldHarmony
Copy link

Yes and here is the output from your new version:

grafik

@asheroto
Copy link
Author

Great, thanks for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment