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.