Thursday, January 23, 2025
HomeProgrammingHow to Get File Version in PowerShell

How to Get File Version in PowerShell

PowerShell is an incredibly powerful scripting language and command-line shell that helps system administrators, developers, and IT professionals automate tasks. One of its many useful capabilities is obtaining file version information, especially for executables or DLL files. Whether you’re managing software versions, troubleshooting, or automating system reports, being able to check the file version in PowerShell is essential.

In this blog post, we’ll learn several methods to get file version information using PowerShell, along with practical examples.

1. Using Get-Item and .VersionInfo

One of the simplest ways to retrieve the version of a file is using the Get-Item cmdlet in PowerShell. This cmdlet allows you to access file properties, including version information.

Here’s an example of how to use Get-Item to retrieve the version of a file:

$file = Get-Item “C:\Path\To\Your\File.exe”
$file.VersionInfo

This will display detailed version information about the file, including:

  • Product Version: The version number for the product the file is part of.
  • File Version: The version number of the file itself.
  • Company Name: The company that created the file.
  • File Description: A description of the file.
  • Product Name: The name of the product associated with the file.
See also  How can I set a default value for an HTML element?

Example Output:

FileVersion : 1.0.0.0 ProductVersion : 1.0.0.0 CompanyName : Example Company FileDescription : Example File Description ProductName : Example Product

2. Using Get-Command

If you are trying to get the version of an executable file (like notepad.exe or any other system application), you can use Get-Command. This is useful if the file is in a directory listed in your system’s $env:PATH.

Example:

(Get-Command notepad.exe).FileVersionInfo

This will output the version information for notepad.exe or whichever executable you specify.

3. Extracting Specific Version Information

If you only need the file version number or the product version, you can extract those specific details. For example, to get just the File Version, you can use the following:

(Get-Item “C:\Path\To\Your\File.exe”).VersionInfo.FileVersion

This will output just the version number, such as:

1.0.0.0

Similarly, if you’re interested in the Product Version, you can run:

(Get-Item “C:\Path\To\Your\File.exe”).VersionInfo.ProductVersion

4. Getting File Version for Multiple Files

If you have several files in a folder and want to get the version information for all of them, you can use Get-ChildItem to list all files and then loop through them:

See also  Reinforcement Learning Tutorial

Get-ChildItem “C:\Path\To\Your\Folder” -Filter *.exe | ForEach-Object {
$_.VersionInfo.FileVersion
}

This will list the version of all .exe files in the specified folder.

5. Getting File Version for Files with Specific Extensions

If you’re dealing with a folder containing multiple types of files (e.g., .exe, .dll, .sys), and you only want to pull the version info for specific file types, you can use the -Filter parameter with Get-ChildItem.

For example, to get the version for all .dll files:

Get-ChildItem “C:\Path\To\Your\Folder” -Filter *.dll | ForEach-Object {
$_.VersionInfo.FileVersion
}

6. Using WMI to Retrieve File Version Information

Another way to get file version information is by querying the file through WMI (Windows Management Instrumentation). Here’s an example of how to do this for a specific file:

Get-WmiObject -Class Win32_LogicalFileSecuritySetting -Filter “FileName=’C:\\Path\\To\\Your\\File.exe'” | Select-Object FileName, Version

This command will return the version information for the specified file.

See also  Learn .Net Framework Tutorial

7. Error Handling

In case the file doesn’t exist or there is an issue with retrieving the version, you can add error handling to your script:

try {
$file = Get-Item “C:\Path\To\Your\File.exe”
$file.VersionInfo.FileVersion
}
catch {
Write-Host “File not found or version info is unavailable.”
}

PowerShell provides a straightforward and efficient way to get file version information, whether you’re dealing with individual files, automating checks across many files, or working with executables located in system directories.

By using cmdlets like Get-Item, Get-Command, and Get-WmiObject, you can easily automate the retrieval of file version information for both management and troubleshooting purposes.

This blog post provides the basics and a few advanced techniques for checking file version information using PowerShell. If you have any specific use cases or questions, feel free to share them in the comments!

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x