How to get OS Version through Powershell with WMI?

79,841

Solution 1

Isn't a simple answer to the original question as follows:

Get-ComputerInfo | select windowsversion

WindowsVersion -------------- 1903

Solution 2

The OS version is stored in a Registry Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ReleaseId. Normally you can read those keys using WMI.

LotPings has provided the correct query in the comments: (Get-Item "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue('ReleaseID')

Solution 3

It's not through the WMI, but Jeff Mercado answer might be to any help anyhow;

Since you have access to the .NET library, you could access the OSVersion property of the System.Environment class to get this information. For the version number, there is the Version property.

For example,

PS C:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
6      1      7601   65536

Details of Windows versions can be found here.

Solution 4

Get-WmiObject can give you the build version and Number like

(Get-WmiObject Win32_OperatingSystem).Version

Or

(Get-WmiObject Win32_OperatingSystem).BuildNumber

If you want a more general information about just the OS, Id rather suggest you using the Get-Item cmdlet, and search "ProductName" and not "relaseID" key as mentioned above.

(Get-Item "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue('ProductName')

The cool thing is that those commands will work with windows 7 up to Windows 10 and Server 2012 up to 2019. It's useful if you need to get information about workstations or apply tasks on workstations in a hybrid environment.

Solution 5

Here is a little script I wrote to find computer information:

Powershell: Get Computer Info

$Computer = "localhost"
$Manufacturer = Get-WmiObject -ComputerName $Computer -class win32_computersystem | select -ExpandProperty Manufacturer
$Model = Get-WmiObject -class win32_computersystem -ComputerName $Computer | select -ExpandProperty model
$Serial = Get-WmiObject -class win32_bios -ComputerName $Computer | select -ExpandProperty SerialNumber
$wmi_os = Get-WmiObject -class Win32_OperatingSystem -ComputerName $Computer | select CSName,Caption,Version,OSArchitecture,LastBootUptime
switch($wmi_os.Version){
'10.0.10240'{$wmi_build="1507"}
'10.0.10586'{$wmi_build="1511"}
'10.0.14393'{$wmi_build="1607"}
'10.0.15063'{$wmi_build="1703"}
'10.0.16299'{$wmi_build="1709"}
'10.0.17134'{$wmi_build="1803"}
'10.0.17686'{$wmi_build="1809"}
}
$wmi_cpu = Get-WmiObject -class Win32_Processor -ComputerName $Computer | select -ExpandProperty DataWidth
$wmi_memory = Get-WmiObject -class cim_physicalmemory -ComputerName $Computer | select Capacity | %{($_.Capacity / 1024kb)}
$DNName = Get-ADComputer -Filter "Name -like '$Computer'" | select -ExpandProperty DistinguishedName
$Boot=[System.DateTime]::ParseExact($($wmi_os.LastBootUpTime).Split(".")[0],'yyyyMMddHHmmss',$null)
[TimeSpan]$uptime = New-TimeSpan $Boot $(get-date)
Write-Host "------Computer Info for $Computer------------------`r"
Write-Host "Hostname from WMI`: $($wmi_os.CSName)"
Write-Host "$DNName"
Write-Host "$Manufacturer $Model SN`:$Serial"
Write-Host "$($wmi_os.Caption) $wmi_build $($wmi_os.OSArchitecture) $($wmi_os.Version)"
Write-Host "CPU Architecture: $wmi_cpu"
Write-Host "Memory: $wmi_memory"
Write-Host "Uptime`: $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
Write-Host "--------------------------------------------------------"
                    
Share:
79,841

Related videos on Youtube

j.walt
Author by

j.walt

Updated on September 18, 2022

Comments

  • j.walt
    j.walt over 1 year

    Does anybody know how to get the OS Version like this:

    OS Version: 1607

    With using Get-WmiObject? Couldn't find this Informatin at all..

    • marijnr
      marijnr almost 6 years
      What is the reason you are using Powershell?
    • j.walt
      j.walt almost 6 years
      I have to implement this information in a desktop information software and the only way I can do this is with an .ini file and a WMI query
    • LotPings
      LotPings almost 6 years
      The powershell tag is still unclear, nevertheless: (Get-Item "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue('ReleaseID')
    • Sandeep
      Sandeep almost 6 years
      Have you tried (Get-WmiObject Win32_OperatingSystem).Version?
    • marijnr
      marijnr almost 6 years
      @Sandeep Version will get you the ID Microsoft uses to identify their Windows version (version 6.1 is used for Windows 7, version 6.2 is used for Windows 10, ...)
  • Christopher Hostage
    Christopher Hostage over 4 years
    And to do it against other PCs in the same domain when you have Admin rights, use "Invoke-Command -ComputerName PCNAME {Get-ComputerInfo | select windowsversion}"
  • Igor Levicki
    Igor Levicki over 2 years
    That assumes you also have remote PowerShell access.