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
Use:
Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption
$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
}
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"
}
}
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"}
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.
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
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}
}