Powershell

Arrays in PowerShell

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:

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:

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

> $NewVar= “varvalue”

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

> $NewVar

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:

>> $laptops

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:

> $laptops[3]

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

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:

> $laptops.length

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

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)

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:

> $laptops

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:

> $laptops=@("one", "two", "three")

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

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:

> $num=@("5", "3", "1", "6", "2", "4")

To print the values above, use the following command:

> $num

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“:

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:

> $num | Sort

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:

> $num = $num | Sort

To print the sorted array, execute the following command:

> $num

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

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”}

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

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

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:

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.

About the author

Adnan Shabbir