Array LIST in Powershell

Hi there!

I was doing my research on Array List in Powershell. Lets start by Google what is an Array List or Examples of Array List in Powershell that way you can understand them better. Here are some of my results:

https://adamtheautomator.com/powershell-aray/

https://www.spguides.com/powershell-arraylist/

https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.3

There are also books on the results and a few more sites but this would solve some of the problems that you might have understanding the Subject.

Array List is a store a list of items in Powershell. Pretty similar to regular arrays except they are more dynamic, meaning in most languages arrays the size is fixed, with an array list, it can grow like a balloon.

TASK 1: Create 4 array lists empty and get the type of it using the .GetType() Method that way you will confirm that it is an array list that you created. One of the list you have to do it without looking, straight out memory.

There are two ways that I found to create array lists. In my two examples I am going to use a booklist, eventually we will add more items to the book list. Below in Powershell are the two ways that I am using to create an array list called $BookList.

Please look at the format and remember how to that way you don’t have to look this stuff up.

$BookList = New-Object -TypeName 'System.Collections.ArrayList';
or
$BookList = [System.Collections.ArrayList]::new()

You can potentially set up an alias in powershell so if you type a letter then the whole array thing comes up and the only thing that you will have to change is the name. I am not that smart so I try to make my life and scripting as simple as possible at the same time it is less work for me. Call me Lazy if you want but why work hard when you can just work smart!! Haha!

Another way to create Arrays List is like this:

[System.Collections.ArrayList]$BookList= @()

If we want to add books to the list we will use the Add() method. for example.

$BookList.Add("Lord of the Rings" )
$BookList.Add("Head First Books")
$BookList.Add("Work Smart and not Hard") 

you can show in the Powershell console what you just added to the list by just typing the name of the variable.

Task2: Add 3 Items to each Array List that you created and at the end show each item of the array using the Foreach method and for loop. example of my booklist foreach below.

foreach ($book in $BookList){ $book }

To count the number of elements inside the Booklist array list I would use the .Count method without the parenthesis works in my case. .

 $BookList.Count

Task3: Get a count using .Count on each array list that you created.

We can also sort the element to the array using sort-Object the way I would sort the books in the booklist would go as follows:

$BookList | Sort-Object

Basically we tell powershell show me all the books then pipe the result of that command and use the cmdlet Sort-Object to sort the result of that command. We can even keep piping it to do more stuff to include sendind the sorted to result to file etc.

Task4: Sort all the Objects of the lists that you created using a Sort-Object Method

You could also convert arrays to array list example from website spguides

$X=2,4,6,8,9,20,5
[System.Collections.ArrayList]$X

or

$X=2,4,6,8,9,20,5
$y=[System.Collections.ArrayList]$X

if we want to remove an Item from the list we will use the removeAt method, so if we want to remove lest say 4 from the array list $y we will use:

$y.RemoveAt(1) 

Task 5 Create a regular array and convert it to an array list.
Task 6: Remove 1 Item from each of the arrays that you created.

If you want to remove a range you will need to use the RemoveRangeMethod which takes two parameters. First parameter is the index number of where to start removing and the second parameter is where to stop removing. for example if we want to Remove

$y.RemoveRange(2,3) 

This is all for now, Remember Worry about the stuff you can control in life and do not worry about the stuff you can’t control.

Have a nice one and hopefully you learned something today!

PEACE!!