Get Operating system name

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,291
Office Version
2016
Platform
Windows
I'm trying to get the name of the Operating system, that is the simple name that most users understand, i.e Windows 7.

I've tried 2 approaches as follows;

This;

Code:
Application.OperatingSystem
produces this;

Windows (32-bit) NT 6.02
and this;

Code:
Public Function getOperatingSystem()
Dim localHost       As String
Dim objWMIService   As Variant
Dim colOperatingSystems As Variant
Dim objOperatingSystem As Variant
On Error GoTo Error_Handler
localHost = "." 'Technically could be run against remote computers, if allowed
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & localHost & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem In colOperatingSystems
getOperatingSystem = objOperatingSystem.Caption & " " & objOperatingSystem.Version
Exit Function
Next
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: getOperatingSystem" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function
produces this;

Windows 10 Pro 10.0.10586
Both of these are generated on a Windows 10 system. I'm being fussy I know, but all I was expecting was simply 'Windows 10' without the extra stuff and I don't understand why the first one produces NT at all.

Is there an easy way of doing this? Or if not can I trim the 2nd one so I just get the simple version?
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
12,011
Office Version
365, 2010
Platform
Windows, Mobile
Or if not can I trim the 2nd one so I just get the simple version?
Does not just changing

Code:
getOperatingSystem = objOperatingSystem.Caption & " " & objOperatingSystem.Version
to

Code:
getOperatingSystem = objOperatingSystem.Caption
Not do what you want?

Edit: and do you not have the word Microsoft in front of
Windows 10 Pro 10.0.10586
 
Last edited:

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,291
Office Version
2016
Platform
Windows
Thanks guys - both suggestions work.
 

Forum statistics

Threads
1,089,174
Messages
5,406,606
Members
403,097
Latest member
danielcarrington

This Week's Hot Topics

Top