How to Get File Version in PowerShell?

Recently, I got a requirement to get the file version in PowerShell. This could be particularly useful when managing software updates, verifying installed application versions, or during software deployment processes. In this blog post, we will explore different methods to get the file version using PowerShell.

To get the file version in PowerShell, you can use the Get-Item cmdlet and access the VersionInfo property of the file object. For example:

$filePath = "C:\MyFolder\File.exe"
$fileVersion = (Get-Item $filePath).VersionInfo.FileVersion

This command retrieves the version information of the specified file located at $filePath.

Get File Version in PowerShell

We will see here 5 different methods to check the file version in PowerShell. Here, I have taken a sample .exe file and we will check the version of this file.

Method 1: Using Get-Command

The Get-Command cmdlet in PowerShell is typically used to get all commands installed on the computer. However, it can also be used to retrieve file version information for executable files.

Here is the complete script.

$filePath = "C:\MyFolder\FirefoxInstaller.exe"
$fileVersion = (Get-Command $filePath).FileVersionInfo.FileVersion
Write-Host "The file version is $fileVersion"

In the script above, we first define the path to the executable file for which we want to get the version information. We then use Get-Command to get the FileVersionInfo property of the file, which includes the version details. Finally, we output the version using Write-Host.

You can see here the output after I executed the script using VS code and it is showing you the file version.

Get File Version in PowerShell

Method 2: Using Get-Item

You can also use the the PowerShell Get-Item cmdlet to get file version information. This cmdlet is used to get the item at a specified location.

$filePath = "C:\MyFolder\FirefoxInstaller.exe"
$fileVersion = (Get-Item $filePath).VersionInfo.FileVersion
Write-Host "The file version is $fileVersion"

Here, we use Get-Item to retrieve the item (file) at the given path and then access its VersionInfo property to get the file version.

Method 3: Using Get-ItemProperty

Get-ItemProperty is another PowerShell cmdlet that retrieves the properties of a specified item. It can also be used to extract the version info.

$filePath = "C:\MyFolder\File.exe"
$fileVersion = (Get-ItemProperty $filePath).VersionInfo.FileVersion
Write-Host "The file version is $fileVersion"

Similar to Get-Item, we use Get-ItemProperty to get the properties of the file and then access the VersionInfo property.

Method 4: Using Get-WmiObject

You can also use Get-WmiObject with the CIM_DataFile class to select the version of a file. This is a more advanced method and can be used to query a lot of information about files on the system.

$filePath = "C:\\Path\\To\\Your\\File.exe"
$fileVersion = (Get-WmiObject -Class CIM_DataFile -Filter "Name='$filePath'").Version
Write-Host "The file version is $fileVersion"

In this script, we use Get-WmiObject to query the CIM_DataFile class for the file at the specified path. We then filter the results by the Name property to match our file path and retrieve the Version property.

Method 5: Using System.Diagnostics.FileVersionInfo

You can also use the .Net classes with PowerShell. You can utilize the System.Diagnostics.FileVersionInfo class to get version information.

$filePath = "C:\MyFolder\File.exe"
$fileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath)
Write-Host "The file version is $($fileVersionInfo.FileVersion)"

This method directly leverages the .NET Framework class FileVersionInfo to get the version information of the file.

Advanced – Reusable Function

You can also write a custom function to check the file version in PowerShell.

Here is a complete script example:

function Get-FileVersion {
    param (
        [string]$filePath
    )

    try {
        if (Test-Path $filePath) {
            $fileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($filePath)
            return $fileVersionInfo.FileVersion
        } else {
            Write-Error "File does not exist: $filePath"
        }
    } catch {
        Write-Error "An error occurred: $_"
    }
}

# Example usage:
$filesToCheck = @(
    "C:\Windows\notepad.exe",
    "C:\Windows\system32\calc.exe"
)

foreach ($file in $filesToCheck) {
    $version = Get-FileVersion -filePath $file
    Write-Host "$file version is $version"
}

This script defines a function Get-FileVersion that takes a file path as input, checks if the file exists, and then retrieves the file version using the .NET FileVersionInfo class. It then iterates over an array of file paths, retrieves their versions, and prints them out.

Conclusion

PowerShell offers multiple ways to retrieve file version information. You can use various PowerShell cmdlets like Get-Command, Get-Item, or Get-ItemProperty, or you want to use Get-WmiObject or the .NET FileVersionInfo class to get the file version.

I hope now you have a complete idea of how to get the file version in PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.