Powershell Parsing Question

I am very new to Powershell and was wondering if someone could help me out with parsing the following command as well as provide an explanation regarding the logic..

$BranchNumber = "2.14Main"
$MajorVersion =  $BranchNumber.Split('.')[0] 
$MinorVersion = $BranchNumber.Split('.')[1] 
$PatchVersion = $BranchNumber.Split('.')[2]
$HotfixVersion = $BranchNumber.Split('.')[3]

Write-Host $MajorVersion $MinorVersion $PatchVersion $HotfixVersion

The Write-Host that I am looking for needs to be the following.

2 14 0 0

September 14th, 2015 5:31pm

There is nothing to explain.  Write--Host outputs the list.

The splits do nothing.

("2.14Main").Split('.')

That is all you get.

Start here to get some background on what you are doing: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 5:41pm

I am very new to Powershell and was wondering if someone could help me out with parsing the following command as well as provide an explanation regarding the logic..

$BranchNumber = "2.14Main"
$MajorVersion =  $BranchNumber.Split('.')[0] 
$MinorVersion = $BranchNumber.Split('.')[1] 
$PatchVersion = $BranchNumber.Split('.')[2]
$HotfixVersion = $BranchNumber.Split('.')[3]

Write-Host $MajorVersion $MinorVersion $PatchVersion $HotfixVersion

The Write-Host that I am looking for needs to be the following.

2 14 0 0

Why do you think that code should do what you want?

Here is a line by line explanation of that code:

Give variable BranchNumber the value of string "2.14Main"


Give variable MajorVersion the value of the first element of the array resulting from the String.Split method using the character . as a delimiter ( '2' is that value )

Give variable MinorVersion the value of the second element of the array resulting from the String.Split method using the character . as a delimiter ( '12Main' is that value )

Give variable PatchVersion the value of the third element of the array resulting from the String.Split method using the character . as a delimiter (there is no third element. so the value of PatchVersion is set to the empty string)

Give variable HotfixVersion the value of the fourth element of the array resulting from the String.Split method using the character . as a delimiter (there is no fourth element so the value of HotfixVersion is set to the empty string)

Output to host the values of variables MajorVersion MinorVersion PatchVersion HotfixVersion with a space between the values.  (this results in
"2 14Main  "
written to the host but without the doublequotes)



September 14th, 2015 9:02pm

I assume you are looking for a logic which returns Major, Minor, build and revision of the given string or something. In your example you have used 2.14Main as input parameter - so you can also try something like below

"2.14Main" -replace '[A-Z]' -as [System.Version]

PS C:\> "2.14Main" -replace '[A-Z]' -as [System.Version]

Major  Minor  Build  Revision
-----  -----  -----  --------
2      14     -1     -1  

We can simply remove the characters in the given string and returns the output as System.Version type. In system.version any digits after the period will be version specific - For example

$a = "2.14Main.4.2" -replace '[A-Z]' -as [System.Version]
$a 

Major  Minor  Build  Revision
-----  -----  -----  --------
2      14     4      2      
This is just my assumption and not sure about your use case. 
Free Windows Admin Tool Kit Click here and download it now
September 15th, 2015 3:21am