问题
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?
回答1:
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.
回答2:
To get the Windows version number, as Jeff notes in his answer, use:
[Environment]::OSVersionIt 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.
Use WMI's Win32_OperatingSystem class (always single instance), for example:
(Get-WmiObject -class Win32_OperatingSystem).Captionwill return something like
Microsoft® Windows Server® 2008 Standard
回答3:
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
回答4:
Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption
Or golfed
gwmi win32_operatingsystem | % caption
Result
Microsoft Windows 7 Ultimate
回答5:
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)
回答6:
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...
回答7:
PS C:\> Get-ComputerInfo | select WindowsProductName, WindowsVersion, OsHardwareAbstractionLayer
returns
WindowsProductName WindowsVersion OsHardwareAbstractionLayer
------------------ -------------- --------------------------
Windows 10 Enterprise 1709 10.0.16299.371
回答8:
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).
回答9:
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 }
回答10:
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.
回答11:
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}
}
回答12:
Use:
Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption
回答13:
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
回答14:
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
回答15:
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name BuildLabEx).BuildLabEx
回答16:
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))"
回答17:
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"
}
}
回答18:
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
回答19:
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
回答20:
[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-----------------#
回答21:
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
回答22:
$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
}
来源:https://stackoverflow.com/questions/7330187/how-to-find-the-windows-version-from-the-powershell-command-line