PoshBytes: The End of Arrays
Learn some surprisingly useful tricks for working with the end of PowerShell arrays. Reverse arrays, grab the last few items, use negative indexes, and skip elements without writing complicated loops.
This post is a companion for the short PoshBytes: The End of Arrays.
Welcome to PoshBytes. Today we’re talking about the end of arrays.
Not the philosophical end of arrays. Arrays are still doing fine.
But let’s be honest would you have clicked on a post titled, “Useful Negative Array Index Tricks”.
Well some of you might have.
But anyways, enough of that, now for the end of arrays.
$array = 1..10
$array
$alpha = 'a'..'z'
$alpha
$random = 1..20 |
Get-Random -Count 20
$random
Bet you didn’t know you could make a character array like that either.
Negative Indexes
Negative indexes count from the end of the array instead of the beginning.
$array[-1]
$array[-2]
$array[-3]
Negative one is the last item.
Negative two is the second-to-last item.
Negative three is the third-to-last item.
It’s much easier than calculating:
$alpha[$alpha.Count - 1]
And much less likely to make your future self stare at the code suspiciously.
Get the Last Five Items
Need the last few values?
$alpha[-5..-1]
This grabs the last five items directly.
No counting.
No loops.
No emotional support calculator.
Get the Largest Five Values
Now here’s an important distinction.
If you want the last five items after sorting:
$random
$random | Sort-Object | Select-Object -Last 5
This doesn’t return the last five positions in the array.
It returns the last five values after sorting, which in this case are the five largest numbers.
So make sure you know if “last” means position or order.
Computers are very literal and rarely accept “you know what I meant.”
Reverse an Array
Want to reverse your array?
Use the .NET Reverse method.
[array]::Reverse($alpha)
$alpha
But be aware, this actually modifies the array in place.
This means that in memory this array is now reversed.
FOREVER!
(well unless the run the command again, but I digress)
Reverse Without Changing the Array
To place you array in reverse without commiting, you can use negative indexes to read the array backwards.
$array[-1..-$array.Count]
$array
The reversed values are displayed, but the original array stays exactly the same.
Sometimes you just want to admire the chaos without committing to it.
Skip the First Item
Need everything except the first element?
You can use array indexing:
$array[1..($array.Count-1)]
Or let Select-Object do the work
$array | Select-Object -Skip 1
Want to skip the first 5 or 20 or whatever, just enter that..
$array | Select-Object -Skip 5
Personally, I prefer the Select-Object version because it is easier to read and less error prone.
Just look at what happens if you exceed the array size for each.
$array[30..($array.Count-1)]
$array | Select-Object -Skip 30
Wrap Up
• Negative indexes count from the end of an array
• $array[-1] returns the last item
• $array[-5..-1] returns the last five items
• [array]::Reverse() modifies the array in place
• Negative index ranges can reverse without modifying the array
• Select-Object -Last works on sorted results
• Select-Object -Skip is a clean way to ignore the first items
• Negative indexes
• Reversing arrays
• Getting the last few items in a collection
• Skipping elements cleanly
• The difference between positional “last” and sorted “last”