locked
How to get PowerShell to see a version number is greater than another? RRS feed

  • Question

  • The script I have written does not seem to see that one version is greater than another. 

    Here are the version numbers that PowerShell seems to be having issues with:

    2.56.5023.942

    2.56.5023.1021 

    Obviously, the version ending in 1021 is greater, but how do I get PowerShell to see that it is?

    For instance, the following shows false, but it is obviously true:

     If ('2.56.5023.1021' -gt '2.56.5023.942')
    {
        $true
    }
    
    Else
    {
        $false
    } 

    Thanks for your help on this. 




    Thursday, March 29, 2018 6:48 PM

Answers

  • You are comparing two strings, so comparing works a little different. You can force the strings to be of type version by doing the folllowing

    if ([Version]'2.56.5023.1021' -gt [Version]'2.56.5023.942'){
        $true
    }else{
        $false
    } 


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, March 29, 2018 6:53 PM

All replies

  • You are comparing two strings, so comparing works a little different. You can force the strings to be of type version by doing the folllowing

    if ([Version]'2.56.5023.1021' -gt [Version]'2.56.5023.942'){
        $true
    }else{
        $false
    } 


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, March 29, 2018 6:53 PM
  • Ahh, I totally forgot about the [version] cast. 

    I will try that. 

    Thanks man. 

    Thursday, March 29, 2018 6:58 PM
  • That worked!

    haha had a brain fart there. 

    Thanks again. 

    Thursday, March 29, 2018 6:59 PM