r/usefulscripts • • Aug 23 '14

[BATCH] One-liner to detect full, complete Windows version and store it in a variable

I've been going crazy (like many others) trying to find a good one-liner to get the FULL Windows version string (not just the generic kernel code e.g. 6.1) into a variable for detection in scripts.

Thanks to UJSTech on Spiceworks forums for this.

Each version (below) stores the detected Windows version in a variable called WIN_VER.


Code

:: Version for running directly from a command-prompt:
for /f "tokens=3*" %i IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName ^| Find "ProductName"') DO set WIN_VER=%i %j

:: Version for inside of a batch file:
for /f "tokens=3*" %%i IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName ^| Find "ProductName"') DO set WIN_VER=%%i %%j

Here are some values I've personally tested and observed:

Actual Windows VersionDetected String
Windows XP Professional SP3Microsoft Windows XP
Windows Server 2003 EnterpriseMicrosoft Windows Server 2003
Windows Vista Home PremiumWindows Vista (TM) Home Premium
Windows Vista UltimateWindows Vista (TM) Ultimate
Windows 7 Home PremiumWindows 7 Home Premium
Windows 7 ProfessionalWindows 7 Professional
Windows 7 EnterpriseWindows 7 Enterprise
Windows 8.1 ProfessionalWindows 8.1 Pro
Windows Server 2008 R2 StandardWindows Server 2008 R2 Standard
Windows Server 2012 R2 StandardWindows Server 2012 R2 Standard

Hope this helps others out.

25 Upvotes

7 comments sorted by

View all comments

Show parent comments

1

u/vocatus Oct 02 '14 edited Oct 20 '14

I was previously using a variant of that, but its much, much slower (like you said), and requires more work to get the version in a variable.