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:32pm

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:42pm

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:03pm

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)



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