Valrok
Valrok

Reputation: 1584

Get operating system without using WMI

I have a Powershell script where I need to get the operating system for various servers. Up until now I've been using WMI in order to accomplish this task, however I've read about how WMI can get timeouts, so I was wondering is there another method of getting the operating system of a server? Here is the code that I use at the moment and want to change in order to avoid WMI:

$serverVersion = Get-WMIObject -computer $server -class Win32_OperatingSystem

Upvotes: 1

Views: 8090

Answers (2)

SimplyKnownAsG
SimplyKnownAsG

Reputation: 934

Depending on how much granularity you need, you could also use .Net's System.Environment.OSVersion:

PS C:\> $os = [Environment]::OSVersion

on windows:

PS C:\> $os
Platform ServicePack    Version        VersionString
-------- -----------    -------        -------------
 Win32NT Service Pack 1 6.1.7601.65536 Microsoft Windows NT 6.1.7601 Service Pack 1

on Linux (Ubuntu 18.04):

PS /home> $os
Platform ServicePack Version   VersionString
-------- ----------- -------   ------------- 
Unix             4.15.0.36 Unix 4.15.0.36

Upvotes: 0

jon Z
jon Z

Reputation: 16646

You will probably find what you are looking for in the registry. As of Windows 2000 the ProductName value can be found in this location: HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion.

To query this registry value using Powershell, try:

(get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName

Upvotes: 4

Related Questions