The elements of an array can use index addressing, the index of the first element is 0, the index of the I element is i-1, and the last element is Count-1, but PowerShell for ease of use, you can direct 1 as the index of the last element.
PS c:powershell> $books = "element 1", "Element 2", "element 3"
PS c:powershell> $books [0]
element 1
PS c:powershell> $ Books[1]
element 2
PS c:powershell> $books [($book. COUNT-1)]
element 3
PS c:powershell> $books [-1]
element 3
Select multiple elements from an array
PS c:powershell> $result =ls
PS c:powershell> $result [0,3,5,12]
Directory:c:powershell
Mode LastWriteTime Length Name
---- ------------- ----------
d---- 2011/11/23 17:25 ABC
- A--- 2011/11/24 20:04 26384 a.txt-a
--- 2011/11/24 20:27 12060 alias.ps1 -A
--- 2011/11/24 17:37 7420 name.html
Reverse the array output
PS c:powershell> $books = "element 1", "Element 2", "element 3"
PS c:powershell> $books [($books. Count). 0]
element 3
element 2
element 1
Adding and removing elements to an array
Because the PowerShell array is stored sequentially in memory, the size of the array must be determined so that the storage space is convenient to allocate, so adding an element to the array is actually equivalent to creating a new array, but then deleting the original copy. Appending elements in the current array can use the "+ =" operator.
PS c:powershell> $books = "element 1", "Element 2", "element 3"
PS c:powershell> $books + + "element 4"
PS c:powershell> $books Element
1
element 2
element 3
element 4
To delete a third element, use:
PS c:powershell> $num =1..4
PS c:powershell> $num
1
2
3
4
PS c:powershell> $ num= $num [0..1]+ $num [3]
PS c:powershell> $num
1
2
4