Adding a comma force to create an array:

$x = @(
    ,@(1,2,3)
)

Simple way:

$x = ,(1,2,3)
Answer from CB. on Stack Overflow
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › easily create and manipulate an array of arrays in powershell
Easily Create and Manipulate an Array of Arrays in PowerShell - Scripting Blog [archived]
December 9, 2011 - Summary: Learn about creating an array of arrays in Windows PowerShell, and see how to store numerical data and rich objects in elements. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday in Charlotte, North Carolina in the United States, it was 60 degrees Fahrenheit (that is 15 degrees ...
Discussions

Issue making array of arrays (of arrays)
It's an array of Objects not an array of arrays. Each line in a csv import is an object, and each cell is a property assigned a value on that object. To build a new line on a csv, you would build a new object, not an array. Like so: $Object = [PSCustomObject]@{ Setting1 = "NewValue1" Setting2 = "NewValue2" Setting3 = "NewValue3" } $Object | Export-CSV Path\to\file.csv -Append -NoTypeInformation You would refer to the cells by property name. $Object.Setting1 More on reddit.com
🌐 r/PowerShell
13
12
January 23, 2018
Powershell: Correctly exporting an "array of arrays" as a CSV file
Hi, I’ve created a simple “array of arrays” to list people’s favorite food to then export to csv as a test. However, when I add a comma as a delimiter between the elements in each “sub array”, I get unexpected results. The header row, first and second rows that were entered at the ... More on community.spiceworks.com
🌐 community.spiceworks.com
11
5
May 17, 2019
PowerShell: How to create a array with mutilple headings

You want some kind of multi-dimensional data structure, not a simple array. In Powershell, the best way to do what you're going for is to create a custom type and then make an array of it.

$a = @()

$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'Col1' -Value 'data1'
$item | Add-Member -type NoteProperty -Name 'Col2' -Value 'data2'
$item | Add-Member -type NoteProperty -Name 'Col3' -Value 'data3'
$item | Add-Member -type NoteProperty -Name 'Col4' -Value 'data4'

$a += $item

# $a[0].Col1 == 'data1'

So now, instead of having just an array, which does not have more than one item per index, you have an array of objects, which are somewhat relational. Does this make sense?

More on reddit.com
🌐 r/sysadmin
18
17
November 20, 2012
How can I build an array of arrays from separate files?
For that Result, you should use Custom Objects File 1: { "ItemName": "MyItem", "Color": "Blue" } File 2: { "ItemName": "YourItem", "Color": "Green" } Now, load the files as variables: $file1 = Get-Content -Path $PathFile1 | ConvertFrom-Json $file2 = Get-Content -Path $PathFile2 | ConvertFrom-Json And now, load both in an array: $thearray = @($file1, $file2) Visualize it $thearray ItemName Color -------- ----- MyItem Blue YourItem Green For more information, see https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.4 More on reddit.com
🌐 r/PowerShell
13
2
August 21, 2024
🌐
PowerShell Forums
forums.powershell.org › powershell help
How to initialize / define an array of arrays - PowerShell Help - PowerShell Forums
June 20, 2022 - I would like to have an array of arrays with, for a start, two string variables. How can I define this or build it via eg. looping? $OrgUnit_A = [pscustomobject]@{ServerName=‘server_1’;FileName=‘Fil_1’}, [pscustomobject]@{ServerName=‘server_2’;FileName=‘Fil_2’} The OrgUnit will ...
🌐
Redmondmag.com
redmondmag.com › articles › 2019 › 01 › 28 › multi-dimensional-powershell-arrays.aspx
How To Create Multi-Dimensional Arrays in PowerShell -- Redmondmag.com
January 28, 2019 - Therefore, each array item is referenced by two numbers: the row number and the position within the list. A position of 1,2 would refer to the third item (because the first item is numbered 0) on the second list, which in this case is Moons. If I actually wanted to display the contents of this position in PowerShell, I would use this command:
🌐
Reddit
reddit.com › r/powershell › issue making array of arrays (of arrays)
r/PowerShell on Reddit: Issue making array of arrays (of arrays)
January 23, 2018 -

UPDATE: Just wanted to thank everyone for their suggestions. I've decided to go with an array of hashtables at basically everyone's urging. Using u/MrECoyne 's simple conversion script, I created a function to replace import-CSV functionality which also does the conversion and outputs a nice clean hashtable. After making just a few minor tweaks to my other functions (thankfully I'm not that far into the project yet) everything looks like it's working nicely again!

I'm sure I'll have some questions later on when I try to do to hashtables what I can usually do to Arrays, but based on what I've seen I'll have full functionality, just maybe in a different way (like the GetEnumator() function for example). Thanks again everyone!

Hey folks,

In a project I'm working on I'm utilizing nested arrays. I'm having trouble where it seems like it being array is lost somewhere. I can create a diagram of tables to help if needed, but my first question is where I think the failure starts -

I'm importing a CSV that looks something like this:

Setting1 Setting2 Setting3
ValueA1 ValueA2 ValueA3
ValueB1 ValueB2 ValueB3

Once imported (using Import-CSV $csvPath), I get what I want to be an array of arrays and what, at least in the console, looks like one. It looks something like this:

Setting1 ValueA1
Setting2 ValueA2
Setting3 ValueA3
Setting1 ValueB1
Setting2 ValueB2
Setting3 ValueB3

Say I save that output to the variable $results. If I call $results.Length I get 2. If I call $results[0] I only get the section that covers ValueA1-A3, and $results[1] is ValueB1-B3.

Calling $results.Setting1 gives me ValueA1 and ValueB1, and $results[0].Setting1 gives me ValueA1 only. All in all it feels like a proper array of arrays.

However, once I try to run array commands on one element I get nothing. $results[0].Length is blank. Running a foreach loop over $results[0] doesn't work. And, in another function, when I try to take it and add it as a member of a custom object to another array, it's added like this:

ArrayName

@{Setting1=ValueA1; Setting2=ValueA2...}
@{Setting1=ValueB1; Setting2=ValueB2...}

And I can no longer dig into them for the individual values (for example, calling $ArrayName[0].Setting1 is empty, as is calling it over the whole thing.

So I'm sure this is going to lead to a few questions from me but the first is - where is it that I'm going wrong at the start? What's being imported when I run import-CSV on a CSV with more than one row of data, and is there a way to properly import it as an array of arrays (since with only one row of data it would import as an array just fine)?

Top answer
1 of 5
5
It's an array of Objects not an array of arrays. Each line in a csv import is an object, and each cell is a property assigned a value on that object. To build a new line on a csv, you would build a new object, not an array. Like so: $Object = [PSCustomObject]@{ Setting1 = "NewValue1" Setting2 = "NewValue2" Setting3 = "NewValue3" } $Object | Export-CSV Path\to\file.csv -Append -NoTypeInformation You would refer to the cells by property name. $Object.Setting1
2 of 5
3
It's not an array of arrays, it's an array of objects, which is why there's no count/length property: $csv = @" Setting1,Setting2,Setting3 ValueA1,ValueA2,ValueA3 ValueB1,ValueB2,ValueB3 "@ | ConvertFrom-Csv foreach ($row in $csv) { "Row: {0} | {1} | {2}" -f $row.Setting1, $row.Setting2, $row.Setting3 } # Output: #Row: ValueA1 | ValueA2 | ValueA3 #Row: ValueB1 | ValueB2 | ValueB3 $csv[0].GetType() # Output: #IsPublic IsSerial Name BaseType #-------- -------- ---- -------- #True False PSCustomObject System.Object If you want an array of arrays (technically this is a list of arrays): $csv = @" Setting1,Setting2,Setting3 ValueA1,ValueA2,ValueA3 ValueB1,ValueB2,ValueB3 "@ | ConvertFrom-Csv $ResultArray = New-Object System.Collections.Generic.List[array] foreach ($row in $csv) { $tempArray = foreach ($property in $row.psobject.Properties.Name) { @{$property=$row.$property} } $ResultArray.Add($tempArray) | Out-Null } $ResultArray[0] # Output: #Name Value #---- ----- #Setting1 ValueA1 #Setting2 ValueA2 #Setting3 ValueA3 $ResultArray[0].Count # Output: #3 If you can post your use case, I'm sure there's a better way of achieving the goal than my example above.
🌐
Medium
davidrbayer.medium.com › multidimensional-arrays-in-powershell-13f4fc77253b
Multidimensional Arrays in PowerShell | by David R Bayer | Medium
April 26, 2015 - You could use an associative array or a hash or a dictionary or similar. My favorite method is to create an array of custom objects.
🌐
Spiceworks
community.spiceworks.com › programming & development
Powershell: Correctly exporting an "array of arrays" as a CSV file - Programming & Development - Spiceworks Community
May 17, 2019 - Hi, I’ve created a simple “array of arrays” to list people’s favorite food to then export to csv as a test. However, when I add a comma as a delimiter between the elements in each “sub array”, I get unexpected results. The header row, first and second rows that were entered at the point of defining the array are fine.
Find elsewhere
🌐
Petri
petri.com › home › powershell › how to use a powershell array
How to Use a PowerShell Array | Petri IT Knowledgebase
October 24, 2022 - First, let’s list the contents of our ‘$FileNamesList’ array: ... Here, I would like to update the 2nd entry (Index ‘1’) from ‘Embroidery’ to ‘Secondhand’. Here’s how to do it: ... One of the most powerful aspects of PowerShell is being able to perform repetitive actions or tasks on many objects at once.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › lang-spec › chapter-09
Arrays - PowerShell | Microsoft Learn
March 24, 2025 - PowerShell supports arrays of one or more dimensions with each dimension having zero or more elements. Within a dimension, elements are numbered in ascending integer order starting at zero.
🌐
Varonis
varonis.com › blog › powershell-array
PowerShell Array Guide: How to Use and Create
June 9, 2022 - Arrays in PowerShell can contain one or more items. An item can be a string, an integer, an object, or even another array, and one array can contain any combination of these items.
🌐
ScriptRunner
scriptrunner.com › en › powershell-tips › understanding-powershell-arrays
Understanding PowerShell Arrays: A Comprehensive Guide
May 22, 2025 - Whenever you store multiple objects in a variable, PowerShell automatically creates an array for you. This array is of the most basic type, capable of storing any type of data, including mixed types: [Object[]].
🌐
YouTube
m.youtube.com › watch
PowerShell Tutorial 3 : Arrays [Beginners]
Share your videos with friends, family, and the world
🌐
Powershellbyexample
powershellbyexample.dev › post › arrays
Arrays | PowerShell By Example
$values = @("One", "Two", "Three") $values # Counting the items in the array using the Count property Write-Host "Items in array $($values.Count)" # Add a value to the array using the + operator $values += "Four" $values Write-Host "Items in array $($values.Count)" # Change a value in the array using the index $values[0] = "Five" $values
🌐
i Love PowerShell
ilovepowershell.com › home › powershell arrays: creating, indexing, and iterating
PowerShell Arrays: Creating, Indexing, and Iterating - i Love PowerShell
March 15, 2023 - So this is probably the most common way that you’ll create an array – you’ll just grab some objects and store them in a variable. But every so often you’ll want to create an empty array, and then add objects to it later. There are a few ways to create an empty array in PowerShell, such as using the @() notation, the New-Object cmdlet, or by casting a range of values as an array.
🌐
Server Academy
serveracademy.com › courses › administration-and-automation-with-windows-powershell › powershell-arrays
PowerShell Arrays
Now, you can think of this example ... the case might be. Kind of gives you an idea. Now, in PowerShell, an Array is a collection of elements that are identified by a unique index number....
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › how to create an array in powershell?
How to Create an Array in PowerShell? - SharePoint Diary
September 20, 2025 - Once you have an empty array, you might want to add items to it dynamically. In PowerShell, this can be achieved using the += operator. For example, $myArray += 1 adds the number 1 to the existing array. However, it’s important to note that the size of an array in PowerShell is immutable once defined.
🌐
YouTube
youtube.com › jackedprogrammer
PowerShell 7 Tutorials for Beginners #3 : Array & ArrayList - YouTube
In this video I will go over Arrays and ArrayList in PowerShell 7.2 and how to compare against each other and which one you should use in your scripts.Tags:P...
Published   September 29, 2022
Views   18K
🌐
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
July 20, 2024 - PowerShell enumerates them all for us and returns a clean list. ... The enumeration still happens but we don't see the complexity behind it. This is where Where-Object comes in so we can filter and select what we want out of the array based on the properties of the object.