Some how-to techniques for PowerShell arrays _powershell

Source: Internet
Author: User

PowerShell An example of an array, PowerShell uses commas to assign values to an array that can hold various types of data values.

PowerShell uses commas to assign values to an array, which can hold various types of data values.

Assignment of an array

Let's look at a simple example below:

Copy Code code as follows:

$myArray = ' Hello ', Get-date, $null, $true
$myArray. Count

There are five elements in this array, with different data types such as String, number, date, null value, Boolean value, and so on.

Data for indexed arrays (positive and reverse order)

Using brackets to index the subscript of an array, such as $myarray[0], represents the first element of the array, the element with the value ' Hello '.

If the subscript of an index is a negative number, it does not mean that the subscript of the array is outside the subscript range, but rather an array of inverted indices, such as $myarray[-1], which represents the last array, which is the array element with the value $true.

PowerShell can select multiple array elements at once, such as: $myArray [0,1,-1], whose value is ' Hello ', $true three values.

Assigning values to an array

The entire array can be PowerShell, and this is a very distinctive feature. Like what:

Copy Code code as follows:
$myArray = $myArray [0,1,-1]

To intercept a part of an array

Copy Code code as follows:

$myArray = $myArray [0..2]

This means taking the 0,1,2 three elements of the array, then forming a new array, and then assigning the value to the $myarray variable.

Count the number of elements in an array

As seen in the previous example, you can use the $myarray.count property to get the number of array elements.

Add an element to the array

Copy Code code as follows:

$myArray + = ' new element '
$myArray. Count

This adds a new element to the array, using the assignment operator = =.

For simple use of arrays, this is a few examples. Note, however, that none of these arrays have data types, followed by a strongly typed array of PowerShell.

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.