2

I want to compare create a function that compare/verifies that a version number is smaller than the another one. The numbers are :

$version1 = 23.0.46.0
$version2 = 24.1.154.0

When I use a simple if condition like below, it does not work because it gives me False

IF(23.0.46.0 -lt 24.1.154.0){
    Write-Host "True"
}
Else
{
    Write-Host "False"
}

I have the idea to split the version number by the points into an array. Then do a loop to compare the parts of each versions until that one is smaller than the another one. In this case it would be directly in the first iteration because 23<24. However I am newly in Powersehll

Thank you for your help

Maikiii
  • 421
  • 4
  • 13

1 Answers1

4

You can use the version class for this. This simple function will return the higher version between 2 inputs:

function Compare-Version {
    param(
        [version]$a,
        [version]$b
    )

    ($a, $b)[$a -lt $b]
}

Compare-Version '25.0.46.0' '25.1.154.0'

Major  Minor  Build  Revision
-----  -----  -----  --------
25     1      154    0

Note that, for the input to be valid, it must comply with the version syntax.

From Remarks of the official documentation:

Version numbers consist of two to four components: major, minor, build, and revision. The major and minor components are required; the build and revision components are optional, but the build component is required if the revision component is defined. All defined components must be integers greater than or equal to 0. The format of the version number is as follows (optional components are shown in square brackets ([ and ]):

major.minor[.build[.revision]]

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • thank's it works when I write it `$test = Compare-Version $version1 $version2`, I do not see a return at the end of your function, however it returns the version ! How does it work ? – Maikiii Dec 14 '21 at 22:31
  • @Maikiii you mean how powershell converts the strings into `version` instances? – Santiago Squarzon Dec 14 '21 at 22:32
  • 2
    @Maikiii: Any output - be it from a PowerShell command or a .NET method call or an expression such as `($a, $b)[$a -lt $b]` - that is neither captured in a variable nor redirected (sent through the pipeline or to a file) is _implicitly output_ from a script or function. See [this answer](https://stackoverflow.com/a/55665963/45375) for more information. – mklement0 Dec 14 '21 at 22:47
  • 1
    Wait, I'm confused in regards to this. Isn't the expression of: `$a -lt $b` returning a boolean? – Abraham Zinala Dec 14 '21 at 23:33
  • 1
    @AbrahamZinala indeed :) what happens when you cast `[int]` to a `bool`? – Santiago Squarzon Dec 14 '21 at 23:34
  • Ello, I'm tracking that it returns 0, or 1, based on the bool value but, what I don't see is how it takes the value of 0 or 1. Lol cause I don't see the cast of type `[int]` here. So the version itself acts as cast since it's properties are of type `[int]`? – Abraham Zinala Dec 15 '21 at 00:19
  • 1
    @AbrahamZinala I guess PS is automatically converting it to `int` whenever it can, it also explains why using a string containing an `int` works, like: `(1, 2)['1']` => `2`. I couldn't find anything specific about it on about_Arrays. – Santiago Squarzon Dec 15 '21 at 00:38
  • 1
    oddly cool man. Thanks!:) – Abraham Zinala Dec 15 '21 at 00:49
  • @Maikiii I think I know what you mean, if you're using the variables from your question `23.0.46.0` without quotes `"` or `'`, both will be null because PS is interpreting it as if you wanted to reference the property of an object. Use `Set-StrictMode -Version 3` to understand what I mean. It should be `$version1 = "23.0.46.0"` instead of `$version1 = 23.0.46.0`. – Santiago Squarzon Dec 15 '21 at 02:03