34

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.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
neves
  • 33,186
  • 27
  • 159
  • 192
  • possible duplicate of [Command line tool to dump Windows DLL version?](http://stackoverflow.com/questions/602802/command-line-tool-to-dump-windows-dll-version) – josh poley Sep 03 '14 at 15:36

8 Answers8

37
wmic datafile where name="C:\\Windows\\System32\\msiexec.exe" get Version /value 

You can use wmic to do it. And you can wrap it into a batch file

@echo off
    setlocal enableextensions

    set "file=%~1"
    if not defined file goto :eof
    if not exist "%file%" goto :eof

    set "vers="
    FOR /F "tokens=2 delims==" %%a in ('
        wmic datafile where name^="%file:\=\\%" get Version /value 
    ') do set "vers=%%a"

    echo(%file% = %vers% 

    endlocal

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 (@this==@isBatch) @then
@echo off
    setlocal enableextensions

    set "file=%~f1"
    if not exist "%file%" goto :eof

    cscript //nologo //e:jscript "%~f0" /file:"%file%"

    endlocal

    exit /b
@end
    var file = WScript.Arguments.Named.Item('file').replace(/\\/g,'\\\\');
    var wmi = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2')
    var files = new Enumerator(wmi.ExecQuery('Select Version from CIM_datafile where name=\''+file+'\'')) 

    while (!files.atEnd()){
        WScript.StdOut.WriteLine(files.item().Version);
        files.moveNext();
    };
    WScript.Quit(0)
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • 1
    Great, but unfortunately you must be in the Administrator group to run it :-( I need to run it as my continuous integration user. – neves Sep 03 '14 at 19:15
  • How do you use %vers% afterwards? When I try `echo %vers%` the command prompt informs me `ECHO is off.` – AlainD May 16 '18 at 17:09
  • @AlainD, the `setlocal / endlocal` prevents changes in the environment to propagate out of the batch file. If you need the `%vers%` value, change the ending `endlocal` line to `enlocal & set "vers=%vers%"` – MC ND May 16 '18 at 19:13
  • @mc-nd small typo: enDlocal & set "vers=%vers% – Andrew Dennison Nov 12 '19 at 15:33
  • 2
    @AndrewDennison, you are right, thank you. Unfortunately I'm not allowed to edit the comment. – MC ND Nov 12 '19 at 21:24
31

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.

(Get-Item -Path 'C:\Program Files\Java\jdk1.8.0_144\bin\java.exe').VersionInfo |
    Format-List -Force

If you must run it in a cmd.exe shell, you could use:

powershell -NoLogo -NoProfile -Command ^
    "(Get-Item -Path 'C:\Program Files (x86)\Java\jre1.8.0_201\bin\java.exe').VersionInfo |" ^
        "Format-List -Force"
lit
  • 14,456
  • 10
  • 65
  • 119
  • 2
    This is the simplest and most clear answer, while directly applicable to PS without any other dependencies. – not2qubit Apr 26 '20 at 06:36
  • @lit but ProductVersion and FileVersion can be empty with PowerShell 7.2.5. – user894319twitter Jul 05 '22 at 04:13
  • @user894319twitter, pwsh.exe has values; `FileVersion : 7.2.5.500, ProductVersion : 7.2.5 SHA: 0aad398b0e918ce7d73dca929ca6395639085b21`. No tool can control if the creator of the file pur information into the field. I am not understanding your point. – lit Jul 05 '22 at 14:15
19
set EXE='c:\firefox\firefox.exe'
powershell "(Get-Item -path %EXE%).VersionInfo.ProductVersion"
bviktor
  • 1,356
  • 13
  • 14
  • While this code-only answer may answer the question, please add an explanation of why it does so. This will help future users evaluate the answer for their situation. – Tom Brunberg Jan 30 '18 at 12:45
  • 7
    "add an explanation of why it does so" Because it is designed to do so? These template questions are pointless. – bviktor Jan 30 '18 at 17:20
  • No, because you have set the current active exe file powershell will interpret with the first line and then you used a powershell command on the currently active exe. – Overmind May 24 '18 at 11:41
  • 3
    What? I'm actively using this snippet in several Jenkins jobs, but please tell me how it's not working, or whatever... – bviktor May 24 '18 at 15:44
  • @bviktor but ProductVersion and FileVersion can be empty with PowerShell 7.2.5. – user894319twitter Jul 05 '22 at 04:05
  • 2
    That's not a PowerShell issue, that's an issue with the executable. If they don't fill in that field, obviously you can't query it. – bviktor Jul 06 '22 at 07:41
12

This will give you only the file version:

wmic datafile where name='c:\\windows\\system32\\notepad.exe' get version

Result:

Version
6.1.7601.18917
MrCalvin
  • 1,675
  • 1
  • 19
  • 27
  • Hi, code-only answers tend to be flagged for moderation. Could you add a line explaining what this does please? – stef Sep 23 '17 at 18:05
  • 2
    This answer should be on top. Short, concise and on point, not to mention working, unlike the current accepted answer! – Björn Larsson Apr 30 '21 at 10:40
  • Is there a way to get the version without needing to pass in the path, i.e. use the directory that the command is run in? – Steve Smith Aug 31 '23 at 15:54
3

and one way with makecab:

; @echo off
;;goto :end_help
;;setlocal DsiableDelayedExpansion
;;;
;;;
;;; fileinf /l list of full file paths separated with ;
;;; fileinf /f text file with a list of files to be processed ( one on each line )
;;; fileinf /? prints the help
;;;
;;:end_help

; REM Creating a Newline variable (the two blank lines are required!)
; set NLM=^


; set NL=^^^%NLM%%NLM%^%NLM%%NLM%
; if "%~1" equ "/?" type "%~f0" | find ";;;" | find /v "find" && exit /b 0
; if "%~2" equ "" type "%~f0" | find ";;;" | find /v "find" && exit /b 0
; setlocal enableDelayedExpansion
; if "%~1" equ "/l" (
;  set "_files=%~2"
;  echo !_files:;=%NL%!>"%TEMP%\file.paths"
;  set _process_file="%TEMP%\file.paths"
;  goto :get_info
; )

; if "%~1" equ "/f" if exist "%~2" (
;  set _process_file="%~2"
;  goto :get_info
; )

; echo incorect parameters & exit /b 1
; :get_info
; set "file_info="

; makecab /d InfFileName=%TEMP%\file.inf /d "DiskDirectory1=%TEMP%" /f "%~f0"  /f %_process_file% /v0>nul

; for /f "usebackq skip=4 delims=" %%f in ("%TEMP%\file.inf") do (
;  set "file_info=%%f"
;  echo !file_info:,=%nl%!
; )

; endlocal
;endlocal
; del /q /f %TEMP%\file.inf 2>nul
; del /q /f %TEMP%\file.path 2>nul
; exit /b 0

.set DoNotCopyFiles=on
.set DestinationDir=;
.set RptFileName=nul
.set InfFooter=;
.set InfHeader=;
.Set ChecksumWidth=8
.Set InfDiskLineFormat=;
.Set Cabinet=off
.Set Compress=off
.Set GenerateInf=ON
.Set InfDiskHeader=;
.Set InfFileHeader=;
.set InfCabinetHeader=;
.Set InfFileLineFormat=",file:*file*,date:*date*,size:*size*,csum:*csum*,time:*time*,vern:*ver*,vers:*vers*,lang:*lang*"

example output (it has a string version which is a small addition to wmic method :) ):

c:> fileinfo.bat /l C:\install.exe
    file:install.exe
    date:11/07/07
    size:562688
    csum:380ef239
    time:07:03:18a
    vern:9.0.21022.8
    vers:9.0.21022.8 built by: RTM
    lang:1033

also you can take a look at tooltipinfo.bat

npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

filever c:\windows\system32\notepad.exe (the filever is preinstalled on every Windows OS).

  • 2
    I typed `where filever` and nothing was found. – neves Nov 09 '20 at 13:24
  • 7
    Apparently, filever is no longer available on Windows 10. Although OP didn't specify the OS version, I'm adding this comment here for future readers. – Ro Yo Mi Dec 07 '20 at 18:26
1

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

@Echo off
Set Version=
Echo WScript.Echo "Set Version=" ^& CreateObject("Scripting.FileSystemObject").GetFileVersion("%SystemRoot%\notepad.exe") >%temp%\~FileVer.vbs
Cscript.exe //nologo %temp%\~FileVer.vbs>%temp%\~FileVer.cmd
Call %temp%\~FileVer.cmd
Echo Version=%Version%

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.

@Echo off
Set Version=
Powershell -c "'Set Version=' + (Get-Command '%SystemRoot%\Notepad.exe').FileVersionInfo.FileVersionRaw.ToString()">%temp%\~FileVer.cmd
Call %temp%\~FileVer.cmd
Echo Version=%Version%

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.

@echo off
Powershell -c "exit 10 + ((Get-Command '%SystemRoot%\Notepad.exe').FileVersionInfo.FileVersionRaw).CompareTo([System.Version] '10.0.19041.1')"
If %ErrorLevel%==9  Echo File needs updating
If %ErrorLevel%==10 Echo File is expected version
If %ErrorLevel%==11 Echo File is newer than expected
JellyHead
  • 11
  • 2
1

Ran this in unelevated PowerShell to get the version of Thunderbird:

(Get-Item -Path "C:\Program Files\Mozilla Thunderbird\thunderbird.exe").VersionInfo.ProductVersion

It outputs the Product Version by filtering it out from the Version Info. Tested it with chrome.exe and it also worked.

Ben
  • 11
  • 3