Converting VBScript's UBound Function

Definition: Returns the largest available subscript for the indicated dimension of an array.


There are at least two ways to determine the index number of the last item in a Windows PowerShell array. For example, suppose we have the following array:

$a = "a","b","c","d","e"

With a 5-item array the last item has an index number of 4 (because the first item in the array is actually item 0). But how do we know that? One way is to use the GetUpperBound() method. This command returns the upper bound for the first dimension (i.e., dimension 0) in the array $a:


You can also get the same results by subtracting one from the array’s Length property:


When you run either command you should get the following:


Return to the VBScript to Windows PowerShell home page