Powershell

Build Better Scripts With PowerShell ArrayLists and Arrays

While using PowerShell, there comes a time to add elements to a script. To do so, an “array” or “ArrayLists” is utilized. An array in PowerShell has a fixed-size data structure, so one needs to mention the element’s size. On the other hand, the ArrayLists do not have a fixed size of elements, so it is required to mention the size of elements, particularly when creating objects. More specifically, arrays can store only a specific data type, while the ArrayLists can store all data types.

This blog will observe a guide to scripts using arrays or ArrayLists.

How to Build/Create Better Scripts With PowerShell Using ArrayLists and Arrays?

You can use the enlisted approaches for creating scripts:

Method 1: Use Arrays to Build Better Scripts With PowerShell

As defined above, an array is a collection of objects. These objects are stored at specific index numbers, which start from “0” and so on. Later, these array objects can be called or invoked using their index numbers. Arrays can be utilized to build PowerShell scripts.

Example 1: Create an Array

This example will show the method to create an array using multiple objects:

$Animals = @('Dog','Cat','Cow')

 

According to the above code:

  • First, initialize a variable, and assign an array starting as “@()”.
  • Inside the array, add three objects separated by commas:

Execute the array assigned variable to verify its existence:

$Animal

 

Example 2: Count the Items in Array

Now, let’s count items of an array:

$Animals.Count

 

According to the above code, first specify the array and concatenate it with the “Count” tag:

Example 3: Get the Type of an Array

An array type can be retrieved by concatenating the array-assigned variable with the “GetType()” method or tag:

$Animals.GetType

 

Method 2: Use ArrayLists to Build Scripts With PowerShell

An “ArrayLists” is also an array type that stores all data types inside it. It does not need to mention element size, unlike arrays. It is associated with the “System.Collections” of the .Net family.

Example 1: Create an ArrayList

This example will demonstrate to create an ArrayList named “SampleList”:

$SampleList = New-Object -TypeName 'System.Collections.ArrayList';
$SampleList.Add("Dog")
$SampleList.Add("Cat")
$SampleList.Add("Cow")

 

According to the above code:

  • First, initialize a variable, and assign the “New-Object” command to create an object.
  • After that, add the “-TypeName” and specify the “Collections.ArrayList” to define the ArrayList.
  • Then, write the variable and invoke the “Add()” method with the value to add as an object inside an array:

Example 2: Count the Number of Objects in ArrayList

This example will get the count number of objects in “ArrayList”:

$SampleList.Count

 

Example 3: Append the Object in an ArrayList

Now, append the object inside an “ArrayList”:

$SampleList += "Goat"

 

Verify whether the object was added to an ArrayList or not:

$SampleList

 

Example 4: Check the Type of ArrayList

Utilize the “GetType()” method for getting the type of ArrayList:

$SampleList.GetType()

 

That was all about building better scripts with PowerShell.

Conclusion

An array stores a single data type and a fixed number of elements inside it, while an ArrayList store all data types and does not store a fixed number of elements. Both of them can help build the PowerShell scripts better. This write-up has elaborated on every detail to build better PowerShell scripts using arrays and ArrayLists.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.