Windows: Command line to read version info of an e

2020-01-31 03:02发布

问题:

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.

回答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)


回答2:

If you are willing and able to use PowerShell...

(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\Java\jdk1.8.0_152\bin\java.exe').VersionInfo ^| ^
        Format-List -Force


回答3:

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


回答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


回答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