How to find the Windows version from the PowerShell command line

WindowsPowershellPowershell 2.0

Windows Problem Overview


How do I find which Windows version I'm using?

I'm using PowerShell 2.0 and tried:

PS C:\> ver
The term 'ver' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify tha
t the path is correct and try again.
At line:1 char:4
+ ver <<<< 
    + CategoryInfo          : ObjectNotFound: (ver:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

How do I do this?

Windows Solutions


Solution 1 - Windows

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 2 - Windows

  1. To get the Windows version number, as Jeff notes in his answer, use:

     [Environment]::OSVersion
    

    It is worth noting that the result is of type [System.Version], so it is possible to check for, say, Windows 7/Windows Server 2008 R2 and later with

     [Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)
    

    However this will not tell you if it is client or server Windows, nor the name of the version.

  2. Use WMI's Win32_OperatingSystem class (always single instance), for example:

     (Get-WmiObject -class Win32_OperatingSystem).Caption
    

    will return something like

    > Microsoft® Windows Server® 2008 Standard

Solution 3 - Windows

Unfortunately most of the other answers do not provide information specific to Windows 10.

Windows 10 has versions of its own: 1507, 1511, 1607, 1703, etc. This is what winver shows.

Powershell:
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId

Command prompt (CMD.EXE):
Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ReleaseId

See also related question on superuser.

As for other Windows versions use systeminfo. Powershell wrapper:

PS C:\> systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List


OS Name             : Microsoft Windows 7 Enterprise
OS Version          : 6.1.7601 Service Pack 1 Build 7601
OS Manufacturer     : Microsoft Corporation
OS Configuration    : Standalone Workstation
OS Build Type       : Multiprocessor Free
System Type         : x64-based PC
System Locale       : ru;Russian
Hotfix(s)           : 274 Hotfix(s) Installed.,[01]: KB2849697,[02]: KB2849697,[03]:...

Windows 10 output for the same command:

OS Name             : Microsoft Windows 10 Enterprise N 2016 LTSB
OS Version          : 10.0.14393 N/A Build 14393
OS Manufacturer     : Microsoft Corporation
OS Configuration    : Standalone Workstation
OS Build Type       : Multiprocessor Free
System Type         : x64-based PC
System Directory    : C:\Windows\system32
System Locale       : en-us;English (United States)
Hotfix(s)           : N/A

Solution 4 - Windows

Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer

returns

WindowsProductName    WindowsVersion OsHardwareAbstractionLayer
------------------    -------------- --------------------------
Windows 10 Enterprise 1709           10.0.16299.371 

Solution 5 - Windows

This will give you the full version of Windows (including Revision/Build number) unlike all the solutions above:

(Get-ItemProperty -Path c:\windows\system32\hal.dll).VersionInfo.FileVersion

Result:

10.0.10240.16392 (th1_st1.150716-1608)

Solution 6 - Windows

Since PowerShell 5:

Get-ComputerInfo
Get-ComputerInfo -Property Windows*

I think this command pretty much tries the 1001 different ways so far discovered to collect system information...

Solution 7 - Windows

If you want to differentiate between Windows 8.1 (6.3.9600) and Windows 8 (6.2.9200) use

(Get-CimInstance Win32_OperatingSystem).Version 

to get the proper version. [Environment]::OSVersion doesn't work properly in Windows 8.1 (it returns a Windows 8 version).

Solution 8 - Windows

I am refining one of the answers

I reached this question while trying to match the output from winver.exe:

Version 1607 (OS Build 14393.351)

I was able to extract the build string with:

,((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx -split '\.') | % {  $_[0..1] -join '.' }  

Result: 14393.351

Updated: Here is a slightly simplified script using regex

(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' |  % { $matches.Values }

Solution 9 - Windows

If you are trying to decipher info MS puts on their patching site such as https://technet.microsoft.com/en-us/library/security/ms17-010.aspx

you will need a combo such as:

$name=(Get-WmiObject Win32_OperatingSystem).caption $bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture $ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId Write-Host $name, $bit, $ver

Microsoft Windows 10 Home 64-bit 1703

Solution 10 - Windows

I took the scripts above and tweaked them a little to come up with this:

$name=(Get-WmiObject Win32_OperatingSystem).caption
$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture

$vert = " Version:"
$ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId

$buildt = " Build:"
$build= (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' |  % { $matches.Values }

$installd = Get-ComputerInfo -Property WindowsInstallDateFromRegistry

Write-host $installd
Write-Host $name, $bit, $vert, $ver, `enter code here`$buildt, $build, $installd

To get a result like this:

Microsoft Windows 10 Home 64-bit Version: 1709 Build: 16299.431 @{WindowsInstallDateFromRegistry=18-01-01 2:29:11 AM}

Hint: I'd appreciate a hand stripping the prefix text from the install date so I can replace it with a more readable header.

Solution 11 - Windows

To produce identical output to winver.exe in PowerShell v5 on Windows 10 1809:

$Version = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
"Version $($Version.ReleaseId) (OS Build $($Version.CurrentBuildNumber).$($Version.UBR))"

Solution 12 - Windows

Use:

Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption

Solution 13 - Windows

As MoonStom says, [Environment]::OSVersion doesn't work properly on an upgraded Windows 8.1 (it returns a Windows 8 version): link.

If you want to differentiate between Windows 8.1 (6.3.9600) and Windows 8 (6.2.9200), you can use (Get-CimInstance Win32_OperatingSystem).Version to get the proper version. However this doesn't work in PowerShell 2. So use this:

$version = $null
try {
    $version = (Get-CimInstance Win32_OperatingSystem).Version
}
catch {
    $version = [System.Environment]::OSVersion.Version | % {"{0}.{1}.{2}" -f $_.Major,$_.Minor,$_.Build}
}

Solution 14 - Windows

Should be easy like this :

Get-ComputerInfo  | select windowsversion

Solution 15 - Windows

This is really a long thread, and probably because the answers albeit correct are not resolving the fundamental question. I came across this site: Version & Build Numbers that provided a clear overview of what is what in the Microsoft Windows world.

Since my interest is to know which exact windows OS I am dealing with, I left aside the entire version rainbow and instead focused on the BuildNumber. The build number may be attained either by:

([Environment]::OSVersion.Version).Build

or by:

(Get-CimInstance Win32_OperatingSystem).buildNumber

the choice is yours which ever way you prefer it. So from there I could do something along the lines of:

    switch ((Get-CimInstance Win32_OperatingSystem).BuildNumber) 
{
    6001 {$OS = "W2K8"}
    7600 {$OS = "W2K8R2"}
    7601 {$OS = "W2K8R2SP1"}    
    9200 {$OS = "W2K12"}
    9600 {$OS = "W2K12R2"}
    14393 {$OS = "W2K16v1607"}
    16229 {$OS = "W2K16v1709"}
    default { $OS = "Not Listed"}
        
}
Write-Host "Server system: $OS" -foregroundcolor Green

Note: As you can see I used the above just for server systems, however it could easily be applied to workstations or even cleverly extended to support both... but I'll leave that to you.

Enjoy, & have fun!

Solution 16 - Windows

Windows PowerShell 2.0:

$windows = New-Object -Type PSObject |
		   Add-Member -MemberType NoteProperty -Name Caption -Value (Get-WmiObject -Class Win32_OperatingSystem).Caption -PassThru |
		   Add-Member -MemberType NoteProperty -Name Version -Value [Environment]::OSVersion.Version                     -PassThru

Windows PowerShell 3.0:

$windows = [PSCustomObject]@{
	Caption = (Get-WmiObject -Class Win32_OperatingSystem).Caption
	Version = [Environment]::OSVersion.Version
}

For display (both versions):

"{0}  ({1})" -f $windows.Caption, $windows.Version 

Solution 17 - Windows

You guys are trying too hard. This works with your local or remote sessions using Enter-PSSession - give it a shot.

All you have to do is type:

cmd ?

Microsoft Windows [Version 10.0.19042.1237]

Solution 18 - Windows

(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx

Solution 19 - Windows

(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64').version

Based off of Tim's earlier answer, the nice thing about this particular location is that the property is already in what I would call a preferred format.

Solution 20 - Windows

A Powershell equivalent of winver

Works for all versions of Windows 10 until 20h2, is fast and not too complex*

function Get-WinVer() {
    $win_release = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").displayversion
    if (!($win_release)) {
        $win_release = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId}
    $win_release
}
Get-WinVer

It shows exactly what winver.exe shows next to "Version".

I didn't expect to have to read so much to come up with this code and I really hope I will not have to tweak it for 22h1 (or what ever the name is at that time).


*: Microsoft certainly made it more complex than it should

Solution 21 - Windows

In addition to other answers, here are some useful information that can be retrieved using PowerShell:

Querying OS & Hardware Info via PowerShell:

Querying General OS (Operating System) Information:

Quickest way to view the OS name:

cmd ?

#Using Get-ComputerInfo:

Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer

#Using Get-WmiObject:

$name=(Get-WmiObject Win32_OperatingSystem).caption
$bit=(Get-WmiObject Win32_OperatingSystem).OSArchitecture
$ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId
Write-Host " OS-Name: `t $name `n Architct: `t $bit  `n Release: `t $ver" 

To list Major Minor Version info:

[System.Environment]::OSVersion.Version 

Querying HostName:

$Env:ComputerName

OR

hostname    #cmd command

Also, if you know the IP address, use the "ping" command (e.g.: ping /a <your_ip_address>) you will see your "hostname" in first line.

Querying Current (Logged-in) User:

whoami    #cmd command

OR

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name 

Querying Mapped Drives: List Mapped Drives - using WMI:

Get-WmiObject -Class Win32_LogicalDisk | Format-Table 

OR

wmic logicaldisk get name       #list just logical-drive letters

OR, to list logical-drive info: FreeSpace, Provider (Real Network Location), Size, and VolumeName:

wmic logicaldisk list brief

List Mapped Drives - using [DriveInfo] class:

[System.IO.DriveInfo]::GetDrives()

List Removable Drives:

$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if ($r) {
    return @($r)[-1]
}

Querying disk capacity, space & Volume-Type

Invoke-Command -ComputerName S1 {Get-PSDrive C} | Select-Object PSComputerName,Used,Free 

Free Space:

(Get-PSDrive C).Free

OR (in GB)

[Math]::Floor(((Get-PSDrive C).Free /[Math]::Pow(2, 30)*10)) /10

Used Space:

(Get-PSDrive C).Used

OR (Used space in GB's)

[Math]::Floor(((Get-PSDrive C).Used /[Math]::Pow(2, 30)*10)) /10

Additionally to view total Space: (in GB)

$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/(1024*1024*1024)
OR
$totalSpace = ((Get-PSDrive C).Used + (Get-PSDrive C).Free)/[Math]::Pow(2, 30)

Rounded off values:

[Math]::Floor($totalSpace*10) / 10
OR
[Math]::Round($totalSpace,1)

Querying Motherboard info:

wmic baseboard get product,Manufacturer,version,serialnumber

Querying Disk Volume (Of Disk Partitions) Info: Get-Volume returns information about storage drive's partitions, e.g.:

Get-Volume                 # All partitions
Get-Volume -DriveLetter C  # Specific partition

#file system type:

Get-Volume -DriveLetter C | select FileSystem
(Get-Volume -DriveLetter C).FileSystem

#partition size:

Get-Volume -DriveLetter C | select Size
OR (in GB)
[Math]::Floor(((Get-Volume -DriveLetter C).Size/[Math]::Pow(2, 30)*10)) /10

Querying Memory / Query RAM

Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum
OR (in GB)
$memory = (Get-WmiObject Win32_PhysicalMemory | Measure -Property Capacity -Sum).Sum
$memory = [Math]::Floor(($memory/[Math]::Pow(2, 30)*10)) /10
$memory.ToString() + " gb"

#Query RAM including Frequency / Speed:

Get-CimInstance win32_physicalmemory | Format-Table Manufacturer,Banklabel,Configuredclockspeed,Devicelocator,Capacity,Serialnumber –autosize

As mentioned, this answer goes bit beyond the question asked, but could be useful for those who'd like additional OS or Hardware information using PowerShell.

Solution 22 - Windows

This will give you the full and CORRECT (the same version number that you find when you run winver.exe) version of Windows (including revision/build number) REMOTELY unlike all the other solutions (tested on Windows 10):

Function Get-OSVersion {
Param($ComputerName)
    Invoke-Command -ComputerName $ComputerName -ScriptBlock {
        $all = @()
        (Get-Childitem c:\windows\system32) | ? Length | Foreach {

            $all += (Get-ItemProperty -Path $_.FullName).VersionInfo.Productversion
        }
        $version = [System.Environment]::OSVersion.Version
        $osversion = "$($version.major).0.$($version.build)"
        $minor = @()
        $all | ? {$_ -like "$osversion*"} | Foreach {
            $minor += [int]($_ -replace".*\.")
        }
        $minor = $minor | sort | Select -Last 1

        return "$osversion.$minor"
    }
}

Solution 23 - Windows

I searched a lot to find out the exact version, because WSUS server shows the wrong version. The best is to get revision from UBR registry KEY.

    $WinVer = New-Object –TypeName PSObject
$WinVer | Add-Member –MemberType NoteProperty –Name Major –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentMajorVersionNumber).CurrentMajorVersionNumber
$WinVer | Add-Member –MemberType NoteProperty –Name Minor –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentMinorVersionNumber).CurrentMinorVersionNumber
$WinVer | Add-Member –MemberType NoteProperty –Name Build –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' CurrentBuild).CurrentBuild
$WinVer | Add-Member –MemberType NoteProperty –Name Revision –Value $(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion' UBR).UBR
$WinVer

Solution 24 - Windows

Using Windows Powershell, it possible to get the data you need in the following way

Caption:

(Get-WmiObject -class Win32_OperatingSystem).Caption

ReleaseId:

(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId

version:

(Get-CimInstance Win32_OperatingSystem).version

Solution 25 - Windows

[solved]

#copy all the code below:
#save file as .ps1 run and see the magic

 Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption
 (Get-CimInstance Win32_OperatingSystem).version
   

#-------------comment-------------#
#-----finding windows version-----#

$version= (Get-CimInstance Win32_OperatingSystem).version
$length= $version.Length
$index= $version.IndexOf(".")
[int]$windows= $version.Remove($index,$length-2)  
$windows
#-----------end------------------#
#-----------comment-----------------#

Solution 26 - Windows

You could also use something like this, by checking the OSVersion.Version.Major:

IF ([System.Environment]::OSVersion.Version.Major -ge 10) {Write-Host "Windows 10 or above"}
IF ([System.Environment]::OSVersion.Version.Major -lt 10) {Write-Host "Windows 8.1 or below"}

Solution 27 - Windows

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Client.OS.rs2.amd64\Version 'For Win 10 Client'

HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Update\TargetingInfo\Installed\Server.OS.amd64\Version 'For Server OS'

Solution 28 - Windows

I wanted to just complete a small script. We used the switch version that was answered before and just elaborated on it. There is no place that will give you the friendly name we are used to. Windows 10 1909 or windows 10 20H2. So we have to program them manually.

$osversion = (Get-CimInstance -class Win32_OperatingSystem).Caption
$buildnumber = (Get-CimInstance Win32_OperatingSystem).BuildNumber
if($osversion -match "Windows 10")
{   
    switch ($buildnumber) 
    { 
        10240 {$OS = "Windows 10 1507"}
        10586 {$OS = "Windows 10 1511"}
        14393 {$OS = "Windows 10 1607"}
        15063 {$OS = "Windows 10 1703"}
        16299 {$OS = "Windows 10 1709"}
        17134 {$OS = "Windows 10 1803"}
        17763 {$OS = "Windows 10 1809"}
        18362 {$OS = "Windows 10 1903"}
        18363 {$OS = "Windows 10 1909"}
        19041 {$OS = "Windows 10 20H1"}
        19042 {$OS = "Windows 10 20H2"}
        19043 {$OS = "Windows 10 21H1"}
        default { $OS = "Not Listed"}
    }
}
if($osversion -match "Windows Server")
{
    switch ($buildnumber) 
    {
        3790 {$OS = "Windows Server 2003 R2"}
        6001 {$OS = "Windows Server 2008"}
        7600 {$OS = "Windows Server 2008 SP1"}
        7601 {$OS = "Windows Server 2008 R2"}    
        9200 {$OS = "Windows Server 2012"}
        9600 {$OS = "Windows Server 2012 R2"}
        14393 {$OS = "Windows Server 2016"}
        17763 {$OS = "Windows Server 2019"}
    }
}
Write-Host "Server system: $OS | $osversion | $buildnumber" -foregroundcolor Green

Now if you want to scan multiple pc's at once like I wanted to use invoke-command or new-pssession Please note Get-WMIObject is depreciated and replaced with get-ciminstance

If you would like an example I can provide later If your using windows 2003 R2 Or earlier.. Stop Move to a new OS.

Solution 29 - Windows

systeminfo at the C:\ prompt in powershell or at the cmd prompt window gives OS name version configuration manufacturer and lots more...

Solution 30 - Windows

$OSVersion = [Version](Get-ItemProperty -Path "$($Env:Windir)\System32\hal.dll" -ErrorAction SilentlyContinue).VersionInfo.FileVersion.Split()[0]

On Windows 10 returns: 10.0.10586.420

You can then use the variable to access properties for granular comparison

$OSVersion.Major equals 10
$OSVersion.Minor equals 0
$OSVersion.Build equals 10586
$OSVersion.Revision equals 420

Additionally, you can compare operating system versions using the following

If ([Version]$OSVersion -ge [Version]"6.1")
   {
       #Do Something
   }

Solution 31 - Windows

You can use python, to simplify things (works on all Windows versions and all other platforms):

import platform

print(platform.system()) # returns 'Windows', 'Linux' etc.
print(platform.release()) # returns for Windows 10 or Server 2019 '10'

if platform.system() = 'Windows':
    print(platform.win32_ver()) # returns (10, 10.0.17744, SP0, Multiprocessor Free) on windows server 2019

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjraraView Question on Stackoverflow
Solution 1 - WindowsJeff MercadoView Answer on Stackoverflow
Solution 2 - WindowsRichardView Answer on Stackoverflow
Solution 3 - WindowsAnton KrouglovView Answer on Stackoverflow
Solution 4 - WindowsLars FosdalView Answer on Stackoverflow
Solution 5 - WindowsIhor ZenichView Answer on Stackoverflow
Solution 6 - WindowsSchadowyView Answer on Stackoverflow
Solution 7 - WindowsMoonStomView Answer on Stackoverflow
Solution 8 - WindowsFaresView Answer on Stackoverflow
Solution 9 - WindowsMichael JoyceView Answer on Stackoverflow
Solution 10 - WindowsRon MVPView Answer on Stackoverflow
Solution 11 - WindowsJames RussellView Answer on Stackoverflow
Solution 12 - WindowsMacView Answer on Stackoverflow
Solution 13 - WindowsmhuView Answer on Stackoverflow
Solution 14 - WindowsBobView Answer on Stackoverflow
Solution 15 - WindowsPorkyView Answer on Stackoverflow
Solution 16 - WindowsVince YpmaView Answer on Stackoverflow
Solution 17 - WindowsMelView Answer on Stackoverflow
Solution 18 - WindowsTestView Answer on Stackoverflow
Solution 19 - WindowsJohn SmithView Answer on Stackoverflow
Solution 20 - WindowsndemouView Answer on Stackoverflow
Solution 21 - WindowsEddie KumarView Answer on Stackoverflow
Solution 22 - WindowsPowerShellGirlView Answer on Stackoverflow
Solution 23 - WindowsAliView Answer on Stackoverflow
Solution 24 - WindowsDan SteveView Answer on Stackoverflow
Solution 25 - WindowsPrime NobelView Answer on Stackoverflow
Solution 26 - WindowsKERRView Answer on Stackoverflow
Solution 27 - WindowsTimView Answer on Stackoverflow
Solution 28 - WindowsDarkSideKillazView Answer on Stackoverflow
Solution 29 - WindowsNewbeeView Answer on Stackoverflow
Solution 30 - WindowsGraceSolutionsView Answer on Stackoverflow
Solution 31 - WindowsRuben KelevraView Answer on Stackoverflow