Check OS with PowerShell

All information relative to the Operating System installed can be retrieved from the registry and more especially in the following key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

.

Until Windows 8.1, the entry CurrentVersion could be used to retrieve the Windows OS version:

.

But, this entry is deprecated since Windows 10, and Microsoft recommends to use the following entries:

  • CurrentMajorVersionNumber (exists only on Win 10)
  • CurrentBuild
  • ProductName
  • ReleaseId (exists only on Win 10)

.

This PowerShell script does the following:

  • Check if the OS is Windows 7 (using the registry CurrentVersion)
  • If not Windows 7, check if the OS is Windows 10 (using the registry CurrentMajorVersionNumber)