Windows: Command line to read version info of an executable file?

66,826

Solution 1

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)

Solution 2

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"

Solution 3

set EXE='c:\firefox\firefox.exe'
powershell "(Get-Item -path %EXE%).VersionInfo.ProductVersion"

Solution 4

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

Solution 5

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

Share:
66,826
Naypa
Author by

Naypa

A Brazilian software developer trying to become a gray beard unix guru hacker. Beard. Check. Gray. Check. Unix. Check. Hacker. Check. Guru. Ops, not yet. 80% done!

Updated on August 02, 2022

Comments

  • Naypa
    Naypa over 1 year

    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.