Comprehensive Guide to Retrieving File Version Information in PowerShell

Nov 22, 2025 · Programming · 12 views · 7.8

Keywords: PowerShell | File Version | .NET Framework | System Administration | Automation Scripting

Abstract: This article provides an in-depth exploration of various methods for obtaining version information from .dll and .exe files in PowerShell, with a focus on technical implementations using the System.Diagnostics.FileVersionInfo class. It covers single file and batch processing scenarios, and thoroughly examines version accuracy and cross-version compatibility issues. Through complete code examples and detailed technical analysis, the article offers practical file version management solutions for system administrators and developers.

Technical Overview of File Version Retrieval in PowerShell

In Windows system administration and automated script development, accurately retrieving version information from executable files is a fundamental and crucial task. PowerShell, as Microsoft's modern scripting language, provides multiple approaches to access version metadata of .dll and .exe files. This article delves into these technical methods, with particular emphasis on efficient implementations using .NET Framework class libraries.

Core Technology and Implementation Principles

PowerShell's strength lies in its deep integration with the .NET Framework. By invoking the System.Diagnostics.FileVersionInfo class, we can directly access the version information resources of Windows executable files. This class encapsulates the Windows API's GetFileVersionInfo function, providing comprehensive access to file version information.

The basic syntax structure is as follows:

[System.Diagnostics.FileVersionInfo]::GetVersionInfo("file_path").FileVersion

Single File Version Information Retrieval

For querying version information of individual files, direct path specification can be used. This method is straightforward and suitable for scenarios where specific file paths are known:

$fileInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\Windows\System32\notepad.exe")
Write-Output "File Version: $($fileInfo.FileVersion)"
Write-Output "Product Version: $($fileInfo.ProductVersion)"
Write-Output "Company Name: $($fileInfo.CompanyName)"
Write-Output "Product Name: $($fileInfo.ProductName)"

Batch File Processing Techniques

In practical system administration work, batch processing of multiple files is often required. PowerShell's pipeline mechanism provides an elegant solution for this:

Get-ChildItem -Path "C:\Windows\System32\*.dll" | ForEach-Object {
    $versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.FullName)
    [PSCustomObject]@{
        FileName = $_.Name
        FileVersion = $versionInfo.FileVersion
        ProductVersion = $versionInfo.ProductVersion
        CompanyName = $versionInfo.CompanyName
        Language = $versionInfo.Language
    }
} | Format-Table -AutoSize

Version Information Accuracy Analysis

It is important to note that different versions of PowerShell exhibit variations in file version information retrieval. In PowerShell 5 and later versions, the FileInfo object includes a new FileVersionRaw property that provides more accurate version information:

# Recommended approach for PowerShell 5+
(Get-Item "C:\Windows\System32\kernel32.dll").VersionInfo.FileVersionRaw

# Traditional approach maintains compatibility
(Get-Item "C:\Windows\System32\kernel32.dll").VersionInfo.FileVersion

Advanced Application: Custom Type Extension

To maintain consistent version information retrieval experience across multiple sessions, custom properties can be added to FileInfo objects using the Update-TypeData command:

Update-TypeData -TypeName System.IO.FileInfo -MemberName FileVersionRaw -MemberType ScriptProperty -Value {
    [System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName) | ForEach-Object {
        [Version](($_.FileMajorPart, $_.FileMinorPart, $_.FileBuildPart, $_.FilePrivatePart) -join ".")
    }
}

Detailed Explanation of Version Information Fields

Complete file version information includes multiple important fields, each serving specific purposes:

Practical Application Scenarios

These techniques have wide-ranging applications in practical work:

  1. Patch Verification: Confirming successful installation of security updates
  2. Version Consistency Checking: Ensuring multiple servers use the same versions of critical components
  3. Software Asset Management: Collecting version information of all executable files in the system
  4. Troubleshooting: Identifying compatibility issues caused by version mismatches

Performance Optimization Recommendations

When processing large numbers of files, performance considerations become important:

# Using Measure-Command for performance testing
$time = Measure-Command {
    Get-ChildItem -Path "C:\Windows\System32\*.dll" | ForEach-Object {
        [System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.FullName)
    }
}
Write-Output "Processing Time: $($time.TotalSeconds) seconds"

Cross-Version Compatibility Handling

To ensure script compatibility across different PowerShell versions, version detection mechanisms are recommended:

function Get-FileVersionInfo {
    param([string]$FilePath)
    
    if ($PSVersionTable.PSVersion.Major -ge 5) {
        return (Get-Item $FilePath).VersionInfo.FileVersionRaw
    } else {
        return [System.Diagnostics.FileVersionInfo]::GetVersionInfo($FilePath).FileVersion
    }
}

Error Handling and Exception Management

Robust scripts should include comprehensive error handling mechanisms:

try {
    $versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath)
    if ($versionInfo -eq $null) {
        throw "Unable to retrieve file version information"
    }
    return $versionInfo.FileVersion
} catch {
    Write-Error "Error processing file $filePath: $($_.Exception.Message)"
    return $null
}

Summary and Best Practices

Retrieving file version information through the System.Diagnostics.FileVersionInfo class is the most reliable and accurate method in PowerShell. In practical work, it is recommended to:

These techniques and methods provide system administrators and developers with powerful file version management capabilities, contributing to the construction of more stable and reliable automated solutions.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.