In order to get the Minor version:
$MinorVersion = ($AdminDisplayVersion -split " ")[1].Substring(3)
In order to get the Build:
$Build = ($AdminDisplayVersion -split " ")[3].Trim(")")
So, now let me break down the whole thing and how I got the pieces.
- How I got the Major version:
–First, I got the AdminDisplayVersion, where everything else starts from. I get the string back.
$AdminDisplayVersion = (Get-ExchangeServer Server1).AdminDisplayVersion
$AdminDisplayVersion
Version 15.0 (Build 1320.4) <--String I'm starting with.
–Next, I use the -split operator to split/cut up the string above into multiple pieces (i.e. an array of strings). I’m splitting the string up using the space character.
$AdminDisplayVersion -split " ")
Version
15.0
(Build
1320.4)
–Next I’m telling PowerShell that I want the second string in the array, which will be at index 1 (since array indexes always start at 0)
($AdminDisplayVersion -split " ")[1]
15.0
–Not sure why, but I couldn’t split that string (i.e. “15.0”) into two strings using the period (.) as the character to split the string on, so I told PowerShell that I wanted a substring of the string that started at position 0 (i.e. where the “1” is in the “15.0” string and that I wanted the substring to be 2 characters in length.
$MajorVersion = ($AdminDisplayVersion -split " ")[1].Substring(0,2)
15
- How I got the Minor version.
–Getting the Minor version works the same as getting the Major version except that this time I told PowerShell that the substring that I wanted (i.e. from the “15.0” string) started at an index of 3 (i.e. where the “0” in the “15.0” string is). By not specifying how long (i.e. how many characters) I wanted the substring to be, it gave me everything from the “0” till the end of the string, which in this case is just the “0”. However, if the original string was “15.1234” (w/o the quotes of course), then the below code would have given me a result of “1234”.
$MinorVersion = ($AdminDisplayVersion -split " ")[1].Substring(3)
$MinorVersion
0
- How I got the Build.
–From the split of the string in the $AdminDisplayVersion, this time I told PowerShell that I wanted the 4th entry that is at index of 3.
($AdminDisplayVersion -split " ")[3]
1320.4)
–Then I just told PowerShell to trim the closing parenthesis off the end.
$Build = ($AdminDisplayVersion -split " ")[3].Trim(")")
$Build
1320.4
So, to put it all together:
$AdminDisplayVersion = (Get-ExchangeServer Server1).AdminDisplayVersion
$MajorVersion = ($AdminDisplayVersion -split " ")[1].Substring(0,2)
$MinorVersion = ($AdminDisplayVersion -split " ")[1].Substring(3)
$Build = ($AdminDisplayVersion -split " ")[3].Trim(")")
Hope that helps and that my explanation makes sense.