How to find the Windows version from the PowerShell command line

前端 未结 25 989
野的像风
野的像风 2020-12-22 15:51

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 o         


        
相关标签:
25条回答
  • 2020-12-22 16:29

    Use:

    Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption
    
    0 讨论(0)
  • 2020-12-22 16:29
    $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
       }
    
    0 讨论(0)
  • 2020-12-22 16:31

    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"
        }
    }
    
    0 讨论(0)
  • 2020-12-22 16:31

    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"}
    
    0 讨论(0)
  • 2020-12-22 16:32
    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

    0 讨论(0)
  • 2020-12-22 16:32

    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}
    }
    
    0 讨论(0)
提交回复
热议问题