We are able to use PowerShell to get the file model of any file in your pc. Getting the model data may be helpful to examine if a software program replace or deployment was profitable, or to stock the software program model on completely different machines.
On this article, I’ll present you find out how to shortly get the file model information, examine if the model is increased or decrease than a specified model quantity, and create a customized perform on your in PowerShell Profile to make it much more simpler.
PowerShell Get File Model
Let’s check out a few strategies to get the file model in PowerShell. Most strategies are just about the identical, so simply use one which is most handy for you. We’re going to use the OneDrive executable for instance for this.
Utilizing Get-Merchandise
Probably the most really helpful option to get the model data is to make use of the Get-Merchandise
cmdlet. The FileVersion property is a part of the VersionInfo property. By putting the Get-Merchandise cmdlet between parentheses, we are able to simply get the whole model quantity:
# Set the file path (will probably be utilizing this in all examples) $file = "C:Program FilesMicrosoft OneDriveOneDrive.exe" # Get model data with Get-Merchandise (Get-Merchandise $file).VersionInfo.FileVersion
As an alternative of the whole file model quantity, we are able to additionally get the uncooked model quantity. This can cut up the quantity up into the most important, minor, construct, and revision numbers.
That is particularly helpful once you solely must examine the most important or minor construct variety of an software.
(Get-Merchandise $file).VersionInfo.FileVersionRaw # Consequence Main Minor Construct Revision ----- ----- ----- -------- 24 181 908 1
Utilizing Get-ChildItem or Get-ItemProperty
As an alternative of the Get-Merchandise cmdlet, we are able to additionally use the Get-ChildItem
or the Get-ItemProperty
cmdlet. As talked about, all 3 strategies are just about the identical:
# Utilizing Get-ChildItem (Get-ChildItem $file).VersionInfo.FileVersion # Utilizing Get-ItemProperty (Get-ItemProperty $file).VersionInfo.FileVersion
I’ve run some assessments to examine if one methodology is quicker, and as anticipated, the distinction between the three is minimal after working 50 assessments:
- Common time for Get-Merchandise: 1.078332 ms
- Common time for Get-ChildItem: 1.108278 ms
- Common time for Get-ItemProperty: 1.02891 ms
Utilizing System.Diagnostics.FileVersionInfo .NET Class
One other methodology to get the file model information is through the use of the System.Diagnostics.FileVersionInfo .NET Class. The benefit of utilizing the .NET class instantly is that it’s sooner than the opposite three strategies.
([System.Diagnostics.FileVersionInfo]::GetVersionInfo($file)).FileVersion
The distinction isn’t actually large, however when you could get the model data for lots of information or examine it on many machines, then this may prevent a while. With the .NET class, it took on common solely 0.79294 ms to get the model data, making it round 20% sooner than the opposite strategies.
Customized Perform Get-FileVersion
If you could examine the file model regularly, then it could be price making a customized perform and storing it in your PowerShell profile. This fashion, you may simply retrieve the model data with an easy-to-remember command, as an alternative of utilizing the Get-Merchandise
cmdlet with all the right properties.
You possibly can copy and paste the perform beneath to your PowerShell profile:
perform Get-FileVersion { param ( [string]$FilePath, [switch]$uncooked = $false ) if (-not (Check-Path $FilePath)) { Write-Error "File not discovered: $FilePath" return } strive { $versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($FilePath) if (-not($uncooked)) { return $versionInfo.FileVersion }else{ return $versionInfo.FileVersionRaw } } catch { Write-Error "Unable to get model information for: $FilePath" } }
This fashion you may simply use the next cmdlet in your PowerShell periods:
Get-FileVersion "C:Program FilesMicrosoft OneDriveOneDrive.exe"
By default, it’ll return the FileVersion quantity for the given file. When you add the parameter -raw, then you’re going to get the model quantity cut up up into main, minor, construct, and revision.
Evaluating File Variations
When you could examine file variations, it’s necessary to forged the model quantity right into a [version] sort. In any other case, will PowerShell do a string comparability, which isn’t that correct together with model numbers.
The [Version]
sort is created to check variations that include a number of numerical elements, like the most important, minor, construct, and revision quantity. This fashion PowerShell is aware of find out how to deal with comparisons between model numbers which might be in any other case not doable with common string comparisons.
# Get the file model and forged it to a [Version] object $model = [Version]((Get-Merchandise $file).VersionInfo.FileVersion) # Outline the goal model for comparability, additionally forged to [Version] $targetVersion = [Version]"24.180.708.1" # Evaluate the variations if ($model -gt $targetVersion) { Write-Host "The file model is newer than 24.180.708.1" } else { Write-Host "The file model is older or equal to 24.180.708.1" }
Wrapping Up
There are a few methods to get the File Model data with PowerShell. Nevertheless, the conventional cmdlets are just about the identical, in terms of usability and efficiency.
If you could retrieve the knowledge from a whole lot of information, then it is smart to the sooner .NET class. Additionally, just remember to check out the customized perform in your PowerShell profile.
Hope you preferred this text, if in case you have any questions, simply drop a remark beneath.