An array is like a data structure to store a fixed size of the same data values. Data structures are a format in which we can organize and store data. Hence, arrays contain and store data value. For example, we have a container, and we divide it into smaller chunks and store some material in each chunk of the container. Arrays work the same way; a large data storage block is divided into a smaller chunk sequentially. Therefore, it is known for storing data in an organized manner.

Moreover, the data stored in the array is of the same type. For instance, we have five small blocks of memory. If the same type of data is stored in all five blocks, we can say that it is a correct array. However, if we keep multiple data types in those five chunks, the arrangement will deviate from the definition of the array. The critical factors in arrays are illustrated in the image below. This diagram shows a memory location and various parameters, such as:

Index: It is a unique identifier for each element of an array. The lower-bound value is 0 (First index), and the upper-bound value is 4 (Last index):

Elements: The data stored at each memory location:

Array Length: Total number of elements in an array:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-01.png" data-lazy- height="397" src="data:image/svg xml,” width=”835″>

Windows PowerShell is a terminal, as well as it supports scripting language too. So, one can perform various programming-related operations using PowerShell. PowerShell does support variable initialization. The variable assignment is quite different from arrays, as arrays can store multiple data values in a single array. While, on the other hand, one variable can hold only one data value at a time. In this article, we will perform various operations related to arrays in PowerShell.

How to Initialize Arrays in PowerShell

At first, we will guide you on the difference between variable initialization and array initialization. The syntax of both terms is almost the same, but the prominent difference comes when we store multiple values in an array.

First, let us open the PowerShell terminal. To do this, click on the “Search” icon on the taskbar, type “PowerShell” here, and run the PowerShell with administrator privileges:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-02-737×1024.png" data-lazy- height="1024" src="data:image/svg xml,” width=”737″>

To initialize a variable, execute the command given below to create a new variable named “NewVar” with a value “varvalue”:

Moreover, to check the variable’s value, write the variable’s name and display the value stored in it:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-03.png" data-lazy- height="233" src="data:image/svg xml,” width=”939″>

However, to initialize an array, you have to change the syntax a little bit. For instance, the following command will initialize an array that stores the names of laptop manufacturing companies:

> $laptops = @(“hp”, “Dell”, “Apple”, “Acer”, “Lenovo”)

To print all the data stored in an array, you have to execute the following command. Notice that printing the array is the same as printing the variable’s value:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-04.png" data-lazy- height="275" src="data:image/svg xml,” width=”939″>

We have stored five data values in the “laptops” array. Thus, the indexing order will be like this:

Index 0 1 2 3 4
Value “hp” “Dell” “Apple” “Acer” “Lenovo”

Moreover, if we want to print the value placed at index number “3”, then you have to run the command given below to get the required value:

As in our case, the “Acer” value is stored at index number 3:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-05.png" data-lazy- height="262" src="data:image/svg xml,” width=”862″>

How to Get the Length and Total Number of Indexes in an Array Using PowerShell

To find the length of an array; you can execute the following command to do so and by taking the previous array “$laptops” as an example:

As there is a total of five values stored in the array, the output should be 5:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-06.png" data-lazy- height="287" src="data:image/svg xml,” width=”852″>

Moreover, you can also check the highest index number in an array. By following the same example as above, there are a total of five values in an array. Thus, the highest index number should be 4. The following command will help to get the upper bound index value:

> $laptops.GetUpperBound(0)

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-07.png" data-lazy- height="206" src="data:image/svg xml,” width=”939″>

How to Append or Replace Data Values of Existing Arrays

If you want to add more values to the existing array, you can use the “ =” instead of only the assignment operator (=). For example, if we want to add another value, “Sony” and “Toshiba“, to our existing array “$laptops“, then the following command can help:

> $laptops = @(“Sony”, “Toshiba”)

After appending the values to the existing array, print the array to verify that the values have been appended or not:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-08.png" data-lazy- height="293" src="data:image/svg xml,” width=”939″>

Moreover, if you want to replace the values of an existing array, you have to use the same syntax as we did for array initialization. If the same name of the array exists, then the data stored in it will replace the existing values:

You can check that these values have replaced the old ones:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-09.png" data-lazy- height="214" src="data:image/svg xml,” width=”939″>

How to Use Sort in Arrays Using PowerShell

Arrays are printed in the order, in which they are stored. If you want to print the values in ascending order, then you can use pipe (|) with the “Sort” command:

For example, we create an array, and the numeric data is stored randomly:

To print the values above, use the following command:

At first, if we print the array, it will show the result as they are stored in the array, starting from index “0” to the last index “5“:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-10.png" data-lazy- height="256" src="data:image/svg xml,” width=”939″>

But if we want to get the output in ascending order, you have to use the following command. Note, this command will not change the order of the data values in an array, but it will only manipulate the output keeping the original data:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-11.png" data-lazy- height="272" src="data:image/svg xml,” width=”939″>

However, if you want to change the order of the array, you have to replace the array values and store them using the “Sort” command. Execute the following command to change the order permanently:

To print the sorted array, execute the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-12.png" data-lazy- height="295" src="data:image/svg xml,” width=”939″>

The “Sort” command usually sorts the numbers or alphabets in ascending order. Here, we will store the values in descending order; a “-descending” flag is used to print or store the values in descending order:

> $num = $num | Sort -descending

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-13.png" data-lazy- height="275" src="data:image/svg xml,” width=”939″>

How to Remove Any Data Value From the Array Using PowerShell

Similar to the “Sort” command, we can use the “where” command to remove any data value from the array. For instance, let us again take the array “$laptops” as an example, and we want to remove “Acer” from the array list. The command given below will only manipulate the output of the array:

> $laptops | where {$_ -ne “Acer”}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-14.png" data-lazy- height="258" src="data:image/svg xml,” width=”939″>

If you want to completely remove the “Acer” from “$laptops“, use the following command to do so:

> $laptops = $laptops | where {$_ -ne “Acer”}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-15.png" data-lazy- height="262" src="data:image/svg xml,” width=”939″>

We can do the same process using the index number instead of typing the name now. If we want to completely remove “Dell“, which is at index “1“, the command for that will be:

> $laptops = $laptops | where {$_ -ne $laptops[1]}

The above command will remove the value placed at index number “1“, which is “Dell” in our case:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/arrays-in-powershell-16.png" data-lazy- height="227" src="data:image/svg xml,” width=”939″>

Conclusion

Arrays store the same data types, and arrays can help you sort the data stored. It is important to note that you can manipulate the output of the array for a specific purpose.

This article briefly demonstrates the handling of arrays using PowerShell. If you are using PowerShell to understand the fundamentals of programming, then this article is beneficial for you to have a good grip on arrays using Windows PowerShell.