Deano Deano - 1 year ago 320
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 Source

Is this what you're looking for?

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download