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:


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

closed as off-topic by Mohamed El-Qassas MVP Dec 17 '17 at 21:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to SharePoint are off-topic here, but can be asked on Stack Overflow." – Mohamed El-Qassas MVP
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    just add $cmID ="" before the for loop and it should be fine. – Damjan Tomic Dec 17 '17 at 12:49
  • This worked for me! – user2598808 Dec 17 '17 at 17:55

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 ";"
$myCollection = "Apple","Pear","Banana","Orange"
$cmID = ""
foreach($item in $myCollection)
    $cmID += $item + ";"
Write-Host $cmID

Not the answer you're looking for? Browse other questions tagged or ask your own question.