spot_img

PowerShell Array

Like other scripting languages, Windows PowerShell also supports a data structure named as an array. An array in a PowerShell is a data structure that is used to store the collection of values in a single variable. The values in an array can be the same type or the different types. A value can be a string, an integer, a generic object, or another array.

Each element or a value in an array has an index. Indexes are the integers which represent the place of a value in an array. We can retrieve the elements of an array using the index. The index of an array always starts with 0, which indicates the first item of an array.

Creating and Initializing an array

  • To declare the array in a PowerShell, assign the multiple values to the variable using the assignment operator. The comma separates the values which are stored in the array.
    The following statement is a syntax to declare the array variable:

Example: To create and initialize an array $k, which contains the five integer values: 10, 15, 20, 11, 5. Type the following command in the PowerShell:

  • We can also initialize the arrays by using the range operator.
    Example: To create and initialize an array $p, which contains the values from 11 to 20. Type the following command in the PowerShell:

Accessing Array Elements

  • You can display all the values of an array on the PowerShell console by typing the name of an array followed by a dollar ($) sign.
    For example: Suppose an array $v contains the values 1,22,33,66,88,99. Then, type the following command on the PowerShell to display all the values of this array:
  • We can also access the elements from an array by using the index number. Enclose the index number in a square bracket.
    The following statement is a syntax to display the value of a specified location:
    $variable_name[index_number]
    For example: Suppose you want to display the second element of an array $p, then type the following command:
  • We can access the part of an array by using the range operator for the index.
    For example: Suppose you want to access from the third element to the sixth element of an array, you would type the following command:
  • We can access the last part of the array by using the negative numbers, which are used to count the elements from the end of an array. The number “-1” denotes the last element of an array.
    Examples: 1. To display the last element of an array, type the following command:

2. To display the last four elements of an array in ascending order of index, type the following command:

Size of an array

  • An Array has a size according to the count of elements. You can get the size of an array by using the following syntax:

For example: If you create a new array $a, which contains the five elements.

$a = 20, 25, 15, 10, 18. To get the size of this array, type the following command:

Manipulation of an Array

  • We can change the value of specific index value in an array by specifying the name of the array and the index number of value to change.
    Example: To change the value of the third element in $p array, type the following command:
  • We can also add the value to the array by using the += operator. Use the following syntax to add a value to an existing array:

For example: To add an element to the array $a, type the following command:

Initializing an empty array

  • We can initialize an empty array by using the following syntax:

Remove element from an Array

  • You can understand how to remove a single or more than one element from an array by using the following example:
    Suppose an array $array contain five elements 5,10,15,20,25. And you want to remove the first and last value from an array, then use the following command:

spot_img
Previous articleOneNote Tutorial: What is OneNote, Features, Alternatives
Next articlePowerShell Hast Table