How to Select the Last Item in an Array in PowerShell?

Do you want to know how to select the last item in an array in PowerShell? In this PowerShell tutorial, I have explained how to select the last element in an array in PowerShell.

To select the last item in an array in PowerShell, use the index -1 with the array variable. For example, if $array is your array variable, $array[-1] will return the last item. This method is effective because PowerShell supports negative indexing, where -1 always refers to the last element.

Method 1: Using Indexing

PowerShell arrays are zero-indexed, which means the first item has an index of 0, the second item has an index of 1, and so on. To get the last item, you can use the index [-1].

$array = 1, 2, 3, 4, 5
$lastItem = $array[-1]
Write-Output $lastItem

This script will output 5, which is the last item in the array.

You can see the output in the screenshot below; I have executed the above PowerShell script using VS code. You can also execute using Windows PowerShell ISE.

Select the Last Item in an Array in PowerShell

Method 2: Using the Select-Object Cmdlet

The Select-Object cmdlet is a versatile tool in PowerShell that can be used to select objects based on different criteria. To get the last item of an array, you can use the -Last parameter.

$array = 'apple', 'banana', 'cherry'
$lastItem = $array | Select-Object -Last 1
Write-Output $lastItem

This will output cherry. For more information on the Select-Object cmdlet, refer to the PowerTip: Select the Next-to-Last Item in a PowerShell Array.

Method 3: Using Array Length

Another way to access the last item is by using the array’s length property in PowerShell. Since the length property returns the total count of items, and arrays are zero-indexed, you need to subtract 1 from the length to get the index of the last item.

$array = 10, 20, 30, 40, 50
$index = $array.Length - 1
$lastItem = $array[$index]
Write-Output $lastItem

This script will output 50.

Method 4: Using the [-1] Index Directly

As mentioned earlier, you can directly use the [-1] index to select the last element of an array without needing to calculate the length.

$array = 'red', 'green', 'blue'
$lastItem = $array[-1]
Write-Output $lastItem

This will output blue. This method is a shorthand for the one above and is typically the preferred way to get the last element due to its simplicity.

Method 5: Using Array Slicing

Array slicing is another feature in PowerShell that can be used to retrieve a subset of an array. By using a range for the indices, you can select the last item.

$array = 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
$lastItem = $array[-1..-1]
Write-Output $lastItem

This will output Friday. The range [-1..-1] effectively slices the last item from the array.

Conclusion

PowerShell provides several ways to select the last item from an array, and the best method to use will depend on your specific needs and preferences. Whether you prefer the simplicity of using the [-1] index or the flexibility of the Select-Object cmdlet, PowerShell has the tools to get the job done.

In this PowerShell tutorial, I have explained how to get the last item from an array in PowerShell using the below methods:

  • Using Indexing
  • Using the Select-Object Cmdlet
  • Using Array Length
  • Using the [-1] Index Directly
  • Using Array Slicing

You may also like: