Get Last Element of Array in PowerShell

Get last element of array in PowerShell

An array is a collection of data elements stored together under a single variable name. Unlike other programming languages, the array in PowerShell contains the values of the same or different data types. It is defined using square brackets [ ] with each element separated by a comma.

The array’s index starts with zero, meaning the array_name[1] represents the second element. One interesting fact is that if you want to retrieve the elements of the array from the last, you can use the minus sign with the index, e.g. the -1 index can be used to retrieve the last element, -2 retrieves the second last element so on and so forth.

Let’s take examples to understand the working of all the methods. For all the methods, the following steps will be followed:

  • Create an array (that is named as $array and contains the values @(1, 2, 3, 4, 5))
  • Get the last element from the array
  • Display the last element as output

Using $array[-1] Index Operator

Use the $array[-1] index operator to get last element of array in PowerShell.

As discussed earlier, this method used the -1 index operator to get the last item in the array. This worked because in PowerShell, the index -1 represents the last item in the array.

Using $array[-1] Index Operator with Parentheses

Use the $array[-1] index operator with parentheses to get last element of array in PowerShell.

This method used the $array[-1] index operator to get the last item in the array, but it also enclosed the array in parentheses to ensure that the index operator is applied to the array itself rather than any other expression that may be used.

Using Select-Object Cmdlet with -Last Parameter

Use the Select-Object cmdlet with the -Last parameter set to 1 to get last element of array in PowerShell.

In this method, the Select-Object cmdlet is used with the -Last parameter set to 1 to get the last item in the array. This cmdlet can select any number of items from an array, including the last.

Using $array.count Property & Index Operator

Use the $array.count property and index operator to get last element of array in PowerShell.

This method uses the .count property of the $array to get the total number of items, then subtracts 1 to get the index of the last item. It then used the index operator to get the last item in the array.

Using Array Class

Use the array class to get last element of array in PowerShell.

This method used the array class to access the Length property and subtracted 1 from it to get the index of the last element in the $array, which we stored in the $last variable. Next, we used the 0..($array.Length - 2) expression to create a range of indices from 0 to the second-to-last item in the $array.

The resulting range was used to select all the items of the original array, excluding the last one, which means we removed the last element from the $array using this expression. Finally, we used the Write-Host cmdlet to print the value of the $last variable.

Using Get_Item() Method

Use the Get_Item() method to get last element of array in PowerShell.

The Get_Item() method gets the item at a specified index from the array. The index, in this case, is calculated by subtracting 1 from the Length property of the $array, which gives the index of the last item.

That’s all about how to get last element of array in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *