Windows Powershell Access Array _powershell

Source: Internet
Author: User
Tags powershell array

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.