Deano Deano - 3 months ago 17
PowerShell Question

PowerShell foreach how to skip last element

I have the follow array with the elements

$list = "A","B","C","1","2","3"


using foreach I can view all items in the array.

foreach ( $item in $list ) { $item }


I would like to print all elements in the array but the last one. As I need to add
;
at the end.

How can I go about doing this?

Answer

Is this what you're looking for?

$List = "A","B","C","1","2","3";
($List[0..($List.Length-2)] -join '') + ';';

Result

ABC12;
Comments