[image] david-zemdegs: Initialise my output array. Most of the times thats unnecessary. [image] david-zemdegs: on each loop initialise an internal array and populate it. Why does it have to be a newly initialized array in each iteration? [image] david-zemdegs: At the end of each … Answer from Olaf on forums.powershell.org
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › powershell tutorials › powershell arraylist – a beginners guide!
PowerShell ArrayList - A Beginners Guide! - SharePoint Diary
September 30, 2025 - Creating an ArrayList in PowerShell is a straightforward process. You can create an empty ArrayList using the following syntax: ... This initializes an empty ArrayList. Alternatively, you can use type accelerators to create a new array list variable:
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › learn › deep-dives › everything-about-arrays
Everything you wanted to know about arrays - PowerShell | Microsoft Learn
... We can create an array and seed it with values just by placing them in the @() parentheses. PS> $data = @('Zero','One','Two','Three') PS> $data.Count 4 PS> $data Zero One Two Three · This array has 4 items.
Discussions

Adding arrays to arraylists
I have an object which is of type {system.collections.arraylist] To add a single element I can use the Add method but to add an array I have to use the ‘+=’ method. Does anyone have a technical explanation for this as ‘+=’ doesnt seem to be in the microsoft doco below: I am also assuming ... More on forums.powershell.org
🌐 forums.powershell.org
7
0
July 11, 2022
Powershell Convert String[] to List<String> - Stack Overflow
To convert a String[] object into a List object in PowerShell, you need to explicitly cast it to such: PS > [string[]]$array = "A","B","C" PS > $array.Gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String[] System.Array PS > PS > [Collections.Ge... More on stackoverflow.com
🌐 stackoverflow.com
How to create an ArrayList from an Array in PowerShell? - Stack Overflow
What do you mean when you say you want to enumerate the files? Why can't you just filter the wanted values into a fresh array? ... Note the comma. I believe what is happening is that when you call a .NET method, you always pass parameters as an array. PowerShell unpacks that array and passes ... More on stackoverflow.com
🌐 stackoverflow.com
When to use array vs array list vs list
Array: When you don't need to dynamically add items to a collection outside a loop. ArrayList: Never. Generic list: When you do need to dynamically add items to a collection outside a loop. Arrays are the default output type in PowerShell. Whenever you run an expression that outputs multiple items you will get a System.Object array. An obvious example of this would be running a command like $Data = Get-ChildItem, a less obvious example would be the output from a loop: $Data = foreach ($Item in $Collection) or even an if/else statement $Data = if ($true) {"Hello"} else {"Goodbye"}. If the expression only outputs a single item then the array will get unwrapped and you just get the raw value like a string. Because it's the default behavior you don't really need to think about it but if you need more complex stuff that can't be handled by simply putting $Var = in front of an expression then you need a list. An easy example to demonstrate is an odd/even list: $Odd = [System.Collections.Generic.List[int]]::new() $Even = [System.Collections.Generic.List[int]]::new() foreach ($Number in 1..10) { if ($Number % 2 -eq 0) { $Even.Add($Number) } else { $Odd.Add($Number) } } More on reddit.com
🌐 r/PowerShell
40
33
April 25, 2022
🌐
Reddit
reddit.com › r/powershell › creating a generic.list from an array, or from scratch quickly.
r/PowerShell on Reddit: Creating a Generic.List from an array, or from scratch quickly.
July 23, 2018 -

u/omers made a good post about Generic.Lists, how they're different to arrays, and when and why you should use them. I'd recommend reading it if you haven't already.


But the post didn't go over one thing, how to create a Generic.List. The most common way, if not the only way I've seen it done is by creating a new list using New-Object, then iterating through an array using foreach or ForEach-Object and adding each element to the list individually.
This has two problems, New-Object is slower than using a type's constructor. And iterating through an array is wasteful.

Instead, [System.Collections.Generic.List]'s constructors should be used. Consider this code:

[Byte[]]$Bytes = 1..100000 | ForEach-Object {Get-Random -Minimum 0 -Maximum 256}

$Output = [PSCustomObject]@{}

$Output | Add-Member -MemberType NoteProperty -Name 'QuickMethod' -Value (
	Measure-Command {
		$QuicklyCreatedGenericList = [System.Collections.Generic.List[Byte]]::New($Bytes)
	}
)

$Output | Add-Member -MemberType NoteProperty -Name 'QuickList' -Value $QuicklyCreatedGenericList

$Output | Add-Member -MemberType NoteProperty -Name 'SlowMethod' -Value (
	 Measure-Command {
		$SlowlyCreatedGenericList = New-Object 'System.Collections.Generic.List[Byte]'
		foreach ($Byte in $Bytes)
		{
			$SlowlyCreatedGenericList.Add($Byte)
		}
	}
)

$Output | Add-Member -MemberType NoteProperty -Name 'SlowList' -Value $SlowlyCreatedGenericList

$Output

This code generates an array of 100,000 random bytes. It then converts the array to a Generic.List using two different methods, the first using [System.Collections.Generic.List]'s constructor, and the second creating a list using New-Object, and iterating through the array, adding each element individually.
The two lists are identical, but the time it took the generate the lists is not. On my machine, creating a list and adding the elements took 196 milliseconds - not too bad, but - using the constructor took 0.0824 milliseconds, or 0.042% the amount of time it took the second method.

Another advantage of this is that it allows you to assign Generic.Lists like arrays, so instead of having a fixed-size:

[String[]]$Gospel = 'PowerShell', 'is', 'good'

you can have a variable-size:

$Gospel = [Collections.Generic.List[String]]::New( [String[]]( 'PowerShell', 'is', 'great' ) )

As noted by u/TheIncorrigible1, you can also cast an array to a list:

$Gospel = [Collections.Generic.List[String]]@('PowerShell', 'is', 'brilliant')

TL;DR?

[Collections.Generic.List[Object]]::New( ( 'A', 'r', 'r', 'a', 'y' ) )    # Is very, very fast.

$List = New-Object 'System.Collections.Generic.List[Object]'
'A', 'r', 'r', 'a', 'y' | ForEach-Object { $List.Add( $_ ) }    # Is very , very slow. (Relatively)
🌐
Varonis
varonis.com › blog › powershell-array
PowerShell Array Guide: How to Use and Create
June 9, 2022 - If you are developing as a PowerShell user, it’s useful to start using ArrayLists instead of straight arrays, but you should also be aware that this type of array is not used by advanced users. Instead, most experienced coders will use a generic list type called List[]. This type of list is a little more complex to use because it is derived straight from C#, but once you’ve mastered it, it offers far more flexibility than the arrays we’ve discussed so far.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Adding arrays to arraylists - PowerShell Help - PowerShell Forums
July 11, 2022 - I have an object which is of type {system.collections.arraylist] To add a single element I can use the Add method but to add an array I have to use the ‘+=’ method. Does anyone have a technical explanation for this as ‘+=’ doesnt seem to be in the microsoft doco below: I am also assuming ...
🌐
O'Reilly
oreilly.com › library › view › windows-powershell-cookbook › 9781449359195 › ch07.html
7. Lists, Arrays, and Hashtables - Windows PowerShell Cookbook, 3rd Edition [Book]
January 10, 2013 - Most scripts deal with more than one thing—lists of servers, lists of files, lookup codes, and more. To enable this, PowerShell supports many features to help you through both its language features and utility cmdlets. PowerShell makes working with arrays and lists much like working with other data types: you can easily create an array or list and then add or remove elements from it.
Author   Lee Holmes
Published   2013
Pages   1034
Find elsewhere
🌐
SPGuides
spguides.com › powershell-arraylist
PowerShell ArrayList [Create and Use]
March 26, 2025 - With ArrayLists, you don’t need to worry about the size of your collection. You can add and remove elements without having to create a new array each time. PowerShell ArrayList is useful that offers flexibility, dynamic resizing, and improved performance when working with collections of objects.
🌐
Wit IT
witit.blog › array-vs-arraylist-powershell
Array vs ArrayList (PowerShell) - Wit IT - witit
December 22, 2022 - First, let’s look at an easy way to define it: [Collections.ArrayList]$ArrayList = @('thing1','thing2') Secondly, let’s check the type with the GetType() method: Sweet! It’s an ArrayList! But why does it matter? Well, let’s check if it’s fixed size or not. Well give me an ice cream cone and call me Joe Biden! An array list isn’t fixed.
🌐
SS64
ss64.com › ps › syntax-arrays.html
Create and use PowerShell Arrays
A PowerShell array holds a list of data items. The data elements of a PowerShell array need not be of the same type, unless the data type is declared (strongly typed). To create an Array just separate the elements with commas.
🌐
Adam the Automator
adamtheautomator.com › powershell-array
PowerShell Arrays, ArrayLists & Collections: Best Practices
Arrays aren’t just relegated to storing strings as shown above. You can also create arrays with other object types like integers. If you need an array of integers in sequential order, you can take a shortcut and use the range .. operator. Below you can see an array was created with the integers 2 through 5 with a single line of code. PS51> $NumberedArray = 2..5 PS51> $NumberedArray 2 3 4 5 · Using a PowerShell ArrayList is also a way in which you can store a list ...
Published   April 22, 2025
🌐
TutorialsPoint
tutorialspoint.com › powershell › powershell_array.htm
Powershell - Array
When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known. Here is a complete example showing how to create, initialize, and process arrays −
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_arrays
about_Arrays - PowerShell | Microsoft Learn
January 3, 2025 - You can use a while loop to display the elements in an array until a defined condition is no longer true. For example, to display the elements in the $a array while the array index is less than 4, type: ... In PowerShell, arrays have three properties that indicate the number of items contained in the array.
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › mastering powershell arrays: a beginner’s guide!
Mastering PowerShell Arrays: A Beginner’s Guide! - SharePoint Diary
December 25, 2020 - Knowing each data structure’s strengths can help you choose the right collection type and determine when to use PowerShell Arrays. Here are the problems I see most often when people start working with arrays. I have hit every one of these myself at some point. These are the habits I have built up over years of writing production scripts. They will save you from common headaches. Initialize arrays with @() before loops to avoid null reference errors when no items match your criteria. Use ArrayList or Generic List for dynamic collections where you add items repeatedly inside a loop.
🌐
LazyAdmin
lazyadmin.nl › home › how to use powershell array – complete guide
How to Use PowerShell Array - Complete Guide — LazyAdmin
January 19, 2023 - Learn how to create and use an Array in PowerShell. Including add and removing items, using the ArrayList and sorting arrays
🌐
Wit IT
witit.blog › easily-turn-a-list-into-an-array-within-powershell
Easily Turn a List into an Array Within PowerShell - Wit IT - witit
December 21, 2024 - Tired of creating CSVs for one-off requests? Here's how you can easily turn a list into an array within PowerShell in only a few simple...