windowsbatch-filecommand-lineversionportable-executable

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.


Solution

  • 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)