1

I want to loop through a collection of items and append semicolon after each item.

Example Output values from mycollection: 5590 6063 1213

Expected Output: 5590;6063;1213

Below is my code.

for ($i = 0; $i -lt $myCollection.Count; $i++) 
{
$cmID =  $cmID + $myCollection[$i].Label + ";"
}
$cmID.Substring(0,$cmsID.Length-1) #This is to remove the last semicolon

Using above code, I am getting output as:

55905590;6063;1213

But I want the output as: 5590;6063;1213

2
  • 1
    just add $cmID ="" before the for loop and it should be fine. Commented Dec 17, 2017 at 12:49
  • This worked for me! Commented Dec 17, 2017 at 17:55

2 Answers 2

1

First of all this is not related to SharePoint so should be on Stack Exchange.

PowerShell does not need to be verbose. You can do what you need very simply:

$myCollection -join ";"
-1
$myCollection = "Apple","Pear","Banana","Orange"
$cmID = ""
foreach($item in $myCollection)
{
    $cmID += $item + ";"
}
Write-Host $cmID

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.