Skip to main content

How to Get a File's Version Information (EXE/DLL) in Batch Script

When managing software deployments or diagnosing issues, you often need to check the specific version of an executable (.exe) or a library (.dll) file. This information is stored in the file's metadata but isn't accessible with simple commands like DIR. The standard, built-in method for retrieving this data in a batch script is through WMI (Windows Management Instrumentation).

This guide will teach you how to use the WMIC command to reliably query file version information, how to parse the output and store it in a variable, and introduce the more direct and modern approach using PowerShell for comparison.

The Core Method: WMIC DATAFILE

The WMIC command-line tool provides a powerful interface to the Windows management layer. We can use it to query the properties of a file, including its version.

The syntax for this operation is: WMIC DATAFILE WHERE Name="C:\\Path\\To\\Your\\File.dll" GET Version

  • DATAFILE: Tells WMIC we are querying file information.
  • WHERE Name="...": Specifies the full path to the file we are interested in.
  • GET Version: Instructs WMIC to return only the Version property of that file.

Basic Example: Getting the Version of a System DLL

Let's query the version of a common system file, kernel32.dll.

@ECHO OFF
ECHO Querying version information for kernel32.dll...
WMIC DATAFILE WHERE Name="C:\\Windows\\System32\\kernel32.dll" GET Version

Output (WMIC produces formatted output, including a header and some extra spacing/lines).

Querying version information for kernel32.dll...
Version
10.0.19041.3570
note

Notice that the backslashes in the path are doubled (\\). This is a critical requirement for the WMIC WHERE clause.

Storing the Version in a Variable

To use this information in a script, we need to capture the output and remove the header and extra lines. A FOR /F loop is perfect for this.

@ECHO OFF
SET "FILE_VERSION="

REM The 'skip=1' option ignores the "Version" header line.
FOR /F "skip=1 delims=" %%V IN (
'WMIC DATAFILE WHERE Name^="C:\\Windows\\System32\\kernel32.dll" GET Version'
) DO (
REM This loop runs twice: once for the version, once for a blank line.
REM We assign the value to the variable. The second run will overwrite it with nothing.
REM A final check is needed.
IF NOT DEFINED FILE_VERSION SET "FILE_VERSION=%%V"
)

ECHO The captured file version is: %FILE_VERSION%

This is a common pattern for parsing WMIC output, but it can be tricky. A slightly refined version is often used to grab the first non-empty line.

A More Robust Parsing Script

@ECHO OFF
SET "FILE_VERSION="
FOR /F "tokens=*" %%V IN (
'WMIC DATAFILE WHERE Name^="C:\\Windows\\System32\\kernel32.dll" GET Version ^| find "."'
) DO (
SET "FILE_VERSION=%%V"
)
ECHO The captured file version is: %FILE_VERSION%
note

This improved version pipes the output to find "." to ensure only lines containing a dot (like a version number) are processed.

A More Powerful Alternative: Using PowerShell

For this task, calling a short PowerShell command from your batch script is significantly simpler and more reliable. It doesn't have the backslash escaping issue and provides direct access to version properties.

@ECHO OFF
SET "TARGET_FILE=C:\Windows\System32\kernel32.dll"
SET "FILE_VERSION="

FOR /F "delims=" %%V IN (
'powershell -Command "(Get-Item '%TARGET_FILE%').VersionInfo.FileVersion"'
) DO (
SET "FILE_VERSION=%%V"
)

ECHO The PowerShell method captured version: %FILE_VERSION%
note

This is the recommended modern approach. It's cleaner, easier to read, and avoids the quirks of WMIC. You can also get other properties like .ProductVersion.

Common Pitfalls and How to Solve Them

Problem: The WMIC Command is Sensitive to Backslashes

This is the most common point of failure. The WMIC WHERE clause requires backslashes in paths to be escaped.

Let's see the error:

REM This will FAIL with an "Invalid query" error.
WMIC DATAFILE WHERE Name="C:\Windows\System32\kernel32.dll" GET Version

Solution: Double Your Backslashes or Use Forward Slashes

Always escape your backslashes by doubling them up (\\). Alternatively, using forward slashes (/) also works.

REM Correct Method 1: Doubled backslashes
WMIC DATAFILE WHERE Name="C:\\Windows\\System32\\kernel32.dll" GET Version

REM Correct Method 2: Forward slashes
WMIC DATAFILE WHERE Name="C:/Windows/System32/kernel32.dll" GET Version

Problem: Parsing the Extra Blank Lines from WMIC Output

As seen in the basic example, WMIC adds extra carriage returns and blank lines to its output, which can make parsing with FOR /F tricky.

Solution: Filter the Output

Piping the WMIC output to find "." or findstr "[0-9]" is a simple and effective way to eliminate the header and blank lines, ensuring that your FOR /F loop only processes the line containing the actual version number. This is shown in the "More Robust Parsing Script" example above.

Problem: Handling "File Not Found"

If the file does not exist, WMIC will return no results, and your variable will not be set.

Solution: Use IF EXIST First

Before attempting a WMIC query, always check if the file exists to provide a clean and immediate error message.

@ECHO OFF
SET "TARGET_FILE=C:\Path\To\non_existent.dll"

IF NOT EXIST "%TARGET_FILE%" (
ECHO [ERROR] File not found: %TARGET_FILE%
GOTO :EOF
)
REM (Proceed with WMIC or PowerShell command here)

Practical Example: Verifying a Minimum File Version

This script checks if a specific DLL meets a minimum version requirement before an application is allowed to run.

@ECHO OFF
SETLOCAL
SET "REQUIRED_VERSION=10"
SET "TARGET_DLL=C:\Windows\System32\uxtheme.dll"
SET "ACTUAL_VERSION="

ECHO Verifying version of %TARGET_DLL%...

FOR /F "tokens=1 delims=." %%A IN (
'powershell -Command "(Get-Item '%TARGET_DLL%').VersionInfo.FileVersion"'
) DO (
SET "MAJOR_VERSION=%%A"
)

IF "%MAJOR_VERSION%" LSS "%REQUIRED_VERSION%" (
ECHO [FAILURE] Required version is %REQUIRED_VERSION% or higher.
ECHO Found version %MAJOR_VERSION%. Please update your system.
) ELSE (
ECHO [SUCCESS] Version check passed (Found: %MAJOR_VERSION%).
)

ENDLOCAL
note

This script uses PowerShell to get the version, then uses FOR /F with a dot delimiter to extract just the major version number for a simple numerical comparison (LSS means "Less Than").

Conclusion

Getting file version information is a task where batch scripting's built-in tools show their age.

  • The WMIC method is the "pure" batch way to do it, but it is cumbersome. You must remember to escape backslashes and carefully parse the messy output.
  • The PowerShell method is superior in every way for this specific task. It is cleaner, more reliable, and easier to read.

For modern scripting on any Windows system, calling the PowerShell one-liner from your batch file is the recommended best practice for its simplicity and power.