Friday, 13 April 2018

How do I determine if my system is Windows 10 using the command line?



I'm working with InstallAnywhere, an old program for creating installers. I would like to install a particular file, only in case of Windows XP and Windows Vista.


Due to InstallAnywhere limitations, this is not possible. I can only decide to delete the file once it has already been installed, based on a rule.


Within that rule, I can check for the platform on which I'm running, which gives me the possibility to check for Windows XP, Windows Vista, Windows 7, etc. but not for Windows 10. I can't say "Perform this action when the system is not XP or Vista", so I need to say "Perform this action on all those platforms, which are not XP or Vista".


However, I can launch command-line commands and catch the result, so here's my question: is there a command which I can use for determining if I'm working on a Windows 10 system?



Answer



Is there a command to determine if I'm working on a Windows 10 system?


You can use wmic.


The following command will return the Windows version.


wmic os get Caption | findstr /v Caption

Example output:


F:\test>wmic os get Caption | findstr /v Caption
Microsoft Windows 7 Home Premium

If you want a little more information, you can use the following batch file (GetOS.cmd), which will retrieve and display:



  • Operating System Version

  • Service Pack Major Version

  • Architecture (64 or 32 bit)


@echo off
setlocal
setlocal enabledelayedexpansion
set _os=
set _sp=
rem use findstr to strip blank lines from wmic output
rem get OS
for /f "usebackq skip=1 tokens=3" %%i in (`wmic os get caption ^| findstr /r /v "^$"`) do (
set "_os=%%i"
)
rem get Service Pack
for /f "usebackq skip=1 tokens=*" %%i in (`wmic os get ServicePackMajorVersion ^| findstr /r /v "^$"`) do (
set "_sp=%%i"
)
rem get Architecture
for /f "usebackq skip=1 tokens=*" %%i in (`wmic OS get OSArchitecture ^| findstr /r /v "^$"`) do (
set "_bits=%%i"
)
echo Operating System Version: %_os%
echo Service Pack Major Version: %_sp%
echo Architecture: %_bits%
endlocal

The OS Version is stored in %_os, Service Pack Major Version is stored in %_sp%, and the Architecture is stored in %_bits%.


Notes:




  • Not completely tested as I don't have all the OS and Service Pack combinations to test it with.




  • The for command retrieves only the 3rd part (token) of the OS. This will work for the desktop versions (if you want to distinguish Server 2008 from other versions you will need to find another solution).




  • %_os will be set to one of the following values: Server, Vista, 7, 8, 8.1 or 10.




Example output:


F:\test>GetOS
Operating System Version: 7
Service Pack Major Version: 1
Architecture: 64-bit

F:\test>



Further Reading



No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...