Skip to content

Instantly share code, notes, and snippets.

@SevenLayerJedi
Last active December 13, 2018 19:51
Show Gist options
  • Save SevenLayerJedi/c0415c03cab1ff51aa49d2f4d708f265 to your computer and use it in GitHub Desktop.
Save SevenLayerJedi/c0415c03cab1ff51aa49d2f4d708f265 to your computer and use it in GitHub Desktop.
# CMD
ver
# .Net Library - OSVersion property of the System.Environment class
[System.Environment]::OSVersion.Version
# .Net Library - OSVersion property of the Environment class
[Environment]::OSVersion
# Check Registry
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ReleaseId).ReleaseId
# Check for "Windows 7/2008 R2" or later
[Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)
# WMI's Win32_OperatingSystem class
(Get-WmiObject -class Win32_OperatingSystem).Caption
# WMI
Get-WmiObject -Class Win32_OperatingSystem | ForEach-Object -MemberName Caption
# SystemInfo
systeminfo /fo csv | ConvertFrom-Csv | select OS*, System*, Hotfix* | Format-List
# hal.dll
(Get-ItemProperty -Path c:\windows\system32\hal.dll).VersionInfo.FileVersion
# CIMInstance
(Get-CimInstance Win32_OperatingSystem).Version
# Registry
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").BuildLabEx -match '^[0-9]+\.[0-9]+' | % { $matches.Values }
# PS
Get-ComputerInfo
Get-ComputerInfo -Property Windows*
# Try Catch
$version = $null
try {
$version = (Get-CimInstance Win32_OperatingSystem).Version
}catch{
$version = [System.Environment]::OSVersion.Version | % {"{0}.{1}.{2}" -f $_.Major,$_.Minor,$_.Build}
}
# WMI
Get-WmiObject -class win32_operatingsystem -computer computername | Select-Object Caption
# PS 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
"{0} ({1})" -f $windows.Caption, $windows.Version
# PS 3.0
$windows = [PSCustomObject]@{
Caption = (Get-WmiObject -Class Win32_OperatingSystem).Caption
Version = [Environment]::OSVersion.Version
}
"{0} ({1})" -f $windows.Caption, $windows.Version
# Combo
$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
# Get OS Version Function
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"
}
}
# Another Solution
$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
# Using Python
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
# Another way
$OSVersion = [Version](Get-ItemProperty -Path "$($Env:Windir)\System32\hal.dll" -ErrorAction SilentlyContinue).VersionInfo.FileVersion.Split()[0]
$OSVersion.Major equals 10
$OSVersion.Minor equals 0
$OSVersion.Build equals 10586
$OSVersion.Revision equals 420
If ([Version]$OSVersion -ge [Version]"6.1")
{
#Do Something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment