- how to tell what version of windows and/or cmd.exe a batch file is running on?
- 8 Answers 8
- Windows: Command line to read version info of an executable file?
- 7 Answers 7
- Check Windows version
- 7 Answers 7
- Get Windows version in a batch file
- 17 Answers 17
- Extract version x.y in a cmd console
- Extract the full version x.y.z
- In a batch script use %% instead of single %
- Version is not always consistent to brand name number
- Command Reference for Windows Subsystem for Linux
- Set WSL 2 as your default version
- Set your distribution version to WSL 1 or WSL 2
- wsl.exe
- Arguments for running Linux commands
- Arguments for managing Windows Subsystem for Linux
- Additional Commands
- wslconfig.exe
- Arguments
- bash.exe
how to tell what version of windows and/or cmd.exe a batch file is running on?
How can one determine what version of Windows and/or cmd.exe a batch file is running on?
There is no cmd /version that I’ve been able to find and the results of SET in a command prompt session don’t give anything obviously unique (between XP and Win7 anyway).
8 Answers 8
The version of cmd.exe should actually be pretty irrelevant, unless you try to use features that didn’t exist before (in command.com for example). There is the pseudovariable
which holds the version of the command extensions which has been 2 for ages (at least back to NT 4, iirc).
But, back to the point: Running ver and parsing the version string might be your best bet:
you can use the «systeminfo» @ cmd.exe
I found a shorter way using ver as well:
Could be even shorter:
I found a shorter way using ver as well:
This will find XP, replace the string with your wanted versions
Type «ver» at a command prompt.
Next time around, since this isn’t really programming related but server or user related, you might try serverfault.com or superuser.com.
To find the windows version using WMIC you can use:
Maybe someone will need the following to determine the SKU (Win7). I’m using some of this script to pick the right OS and XML during sysprep. Hope it helps!
The internal command ver reports windows version number (which could have been learned by typing help at the command prompt).
There is a dynamic variable %CMDEXTVERSION% , but it hasn’t progressed in several releases so it’s only useful for delineating between Windows NT and Windows 2000 and newer. (Thanks @Joey, here.)
Here’s a batch to parse the output of ver for XP and newer, courtesy of Simon Sheppard:
And here’s my own fairly complete, largely academic, kick at the can which returns the parsed version number as environment variables:
Windows: Command line to read version info of an executable file?
Does Windows have an executable that I can run in the command shell which returns the version number of an executable (.exe) file?
I see a lot of questions that show how to do it from different languages, and references to third party software to write it, but I can’t find a simple shell command to do it. Additional points if I don’t need to install anything.
It must be run as normal user. Not administrator.
7 Answers 7
You can use wmic to do it. And you can wrap it into a batch file
Save it as (example) getVersion.cmd and call as getVersion.cmd «c:\windows\system32\msiexec.exe»
edited to adapt to comments and not require administrator rights. In this case, an hybrid cmd/javascript file is used to query wmi. Same usage
If you are willing and able to use PowerShell, the following code will work. If you are on a supported Windows system, PowerShell will be available.
If you must run it in a cmd.exe shell, you could use:
This will give you only the file version:
and one way with makecab :
example output (it has a string version which is a small addition to wmic method 🙂 ):
also you can take a look at tooltipinfo.bat
filever c:\windows\system32\notepad.exe (the filever is preinstalled on every Windows OS).
filever.exe is in SUPPORT.CAB from the Windows 2003 Support tools, and maybe other places.
A method using VBScript and Scripting.FileSystemObject from a CMD script
A variant of the powershell method, if you are calling from a CMD script. Using FileVersionRaw instead of FileVersion, because FileVersion can have extra text decoration, but have to ToString() to get the expected format.
A Powershell from CMD method to compare versions, as that could be the reason for asking in the first place. Have to use %ErrorLevel%==x because ErrorLevel==x is actually greater or equal.
Check Windows version
How I can check in C++ if Windows version installed on computer is Windows Vista and higher (Windows 7)?
7 Answers 7
Similar to other tests for checking the version of Windows NT:
All the answers in this thread point you to using GetVersion or GetVersionEx for this test, which is incorrect. It seems to work, but it is risky. The primary source of appcompat problems for Windows OS upgrades comes from poorly written tests based on GetVersion results with bad assumptions or buggy comparisons.
The correct way to do this test is to use VerifyVersionInfo , not GetVersion or GetVersionEx .
If you are using the VS 2013 compiler toolset and the Windows 8.1 SDK, you can use the VersionHelpers.h and just call IsWindowsVistaOrGreater .
If you are using the VS 2013 v120_xp platform toolset to target Windows XP, you are actually using the Windows 7.1A SDK, so you need to use VeriyVersionInfo directly.
This code will work on Windows 2000 or later and give you a robust result. If you really needed this test to run on Windows 98 or Windows ME -and- you are using a compiler toolset old enough to actually run on that platform, you’d do the same test but with explicit rather than implicit linking. What’s in a version number?
Furthermore, using GetVersion or GetVersionEx will by default get the wrong version on Windows 8.1 and Windows 10. See Manifest Madness.
Get Windows version in a batch file
I need to get the OS version with a batch file. I ‘ve seen a lot of examples online, many uses something like this code:
The problem is when I execute this on Vista or Windows 7 I get the message
Is there any other way to do what I want?
17 Answers 17
Have you tried using the wmic commands?
Try wmic os get version
This will give you the version number in a command line, then you just need to integrate into the batch file.
It’s much easier (and faster) to get this information by only parsing the output of ver :
This table on MSDN documents which version number corresponds to which Windows product version (this is where you get the 6.1 means Windows 7 information from).
The only drawback of this technique is that it cannot distinguish between the equivalent server and consumer versions of Windows.
These one-line commands have been tested on Windows XP, Server 2012, 7 and 10 (thank you Mad Tom Vane).
Extract version x.y in a cmd console
Extract the full version x.y.z
In a batch script use %% instead of single %
Version is not always consistent to brand name number
Be aware that the extracted version number does not always corresponds to the Windows name release. See below an extract from the full list of Microsoft Windows versions.
10.0 Windows 10
6.3 Windows Server 2012
6.3 Windows 8.1 /!\
6.2 Windows 8 /!\
6.1 Windows 7 /!\
6.0 Windows Vista
5.2 Windows XP x64
5.1 Windows XP
5.0 Windows 2000
4.10 Windows 98
Please also up-vote answers from Agent Gibbs and peterbh as my answer is inspired from their ideas.
I know it’s an old question but I thought these were useful enough to put here for people searching.
This first one is a simple batch way to get the right version. You can find out if it is Server or Workstation (if that’s important) in another process. I just didn’t take time to add it.
We use this structure inside code to ensure compliance with requirements. I’m sure there are many more graceful ways but this does always work.
This second one isn’t what was asked for but it may be useful for someone looking.
Here is a VBscript function that provides version info, including if it is the Server (vs. workstation).
So the answers I read here are mostly lengthy. The shortest answer by Alex works good, but not so clean.
Best part is that unlike others’, it works fast.
I came up with my own one-liner:
Above is for command prompt. To make it work in batch file, replace all % with %% to make it look like:
In my computer, it simply produces Windows 10 Pro
Here is another variant : some other solutions doesn’t work with XP, this one does and was inspired by RLH solution.
This script will continue only if it detects the Windows version you want, in this example I want my script to run only in win 7, so to support other windows just change the GOTO :NOTTESTEDWIN to GOTO :TESTEDWIN
This will return 5.1 for xp or 6/1 for windows 7
Here’s my one liner to determine the windows version:
This returns the windows version, i.e., XP, Vista, 7, and sets the value of «ver» to equal the same.
Actually, that one liner doesn’t work for windows xp since the ver contains xp in the string. Instead of 5.1 which you want, you would get [Version.5 because of the added token.
I modified the command to look like this: for /f «tokens=4-6 delims=[. » %%i in (‘ver’) do set VERSION=%%i.%%j
This will output Version.5 for xp systems which you can use to indentify said system inside a batch file. Sadly, this means the command cannot differentiate between 32bit and 64bit build since it doesn’t read the .2 from 5.2 that denotes 64bit XP.
You could make it assign %%k that token but doing so would make this script not detect windows vista, 7, or 8 properly as they have one token less in their ver string. Hope this helps!
Command Reference for Windows Subsystem for Linux
The best way to interact with the Windows Subsystem for Linux is to use the wsl.exe command.
Set WSL 2 as your default version
Run the following command in Powershell to set WSL 2 as the default version when installing a new Linux distribution:
If you’re running a 32-bit process in order to access wsl.exe (a 64-bit tool), you may have to run the above command in the following manner: C:\Windows\Sysnative\wsl.exe —set-default-version 2
Set your distribution version to WSL 1 or WSL 2
You can check the WSL version assigned to each of the Linux distributions you have installed by opening the PowerShell command line and entering the command (only available in Windows Build 19041 or higher): wsl -l -v
To set a distribution to be backed by either version of WSL please run:
Make sure to replace with the actual name of your distribution and with the number ‘1’ or ‘2’. You can change back to WSL 1 at anytime by running the same command as above but replacing the ‘2’ with a ‘1’.
Additionally, if you want to make WSL 2 your default architecture you can do so with this command:
This will set the version of any new distribution installed to WSL 2.
wsl.exe
Below is a list containing all options when using wsl.exe as of Windows Version 1903.
Using: wsl [Argument] [Options. ] [CommandLine]
Arguments for running Linux commands
Without arguments
If no command line is provided, wsl.exe launches the default shell.
—exec, -e
Execute the specified command without using the default Linux shell.
Pass the remaining command line as is.
The above commands also accept the following options:
—distribution, -d
Run the specified distribution.
—user, -u
Run as the specified user.
Arguments for managing Windows Subsystem for Linux
—export
Exports the distribution to a tar file. The filename can be — for standard output.
—import
Imports the specified tar file as a new distribution. The filename can be — for standard input.
—list, -l [Options]
—all
List all distributions, including distributions that are currently being installed or uninstalled.
—running
List only distributions that are currently running.
—set-default, -s
Sets the distribution as the default.
—terminate, -t
Terminates the specified distribution.
—unregister
Un-register the distribution.
—help Display usage information.
Additional Commands
There are also historic commands to interact with the Windows Subsystem for Linux. Their functionality is encompassed within wsl.exe , but they are still available for use.
wslconfig.exe
This command lets you configure your WSL distribution. Below is a list of its options.
Using: wslconfig [Argument] [Options. ]
Arguments
/l, /list [Options]
Lists registered distributions.
/all Optionally list all distributions, including distributions that are currently being installed or uninstalled.
/running List only distributions that are currently running.
/s, /setdefault Sets the distribution as the default.
/t, /terminate Terminates the distribution.
/u, /unregister Un-registers the distribution.
/upgrade Upgrades the distribution to the WslFs file system format.
bash.exe
This command is used to start a bash shell. Below are the options you can use with this command.
Using: bash [Options. ]
No Option given
Launches the Bash shell in the current directory. If the Bash shell is not installed automatically runs lxrun /install
launches the bash shell into the user’s home directory. Similar to running cd
-c » «
Runs the command, prints the output and exits back to the Windows command prompt.