try Something like this:

$array1=import-csv "C:\temp\log.csv"
$array2=import-csv "C:\temp\log2.csv"

#modify founded and output not founded
$toadd=$array2 | %{
$current=$_
$founded=$array1 | where pc -eq $current.pc | %{$_.date=$current.date;$_}

    if ($founded -eq $null)
    {
      $current.city='UNKNOW' 
      $current 
    }
}

#output of $array1 modified and elements to add
$array1, $toadd
Answer from Esperento57 on Stack Overflow
Top answer
1 of 2
2

try Something like this:

$array1=import-csv "C:\temp\log.csv"
$array2=import-csv "C:\temp\log2.csv"

#modify founded and output not founded
$toadd=$array2 | %{
$current=$_
$founded=$array1 | where pc -eq $current.pc | %{$_.date=$current.date;$_}

    if ($founded -eq $null)
    {
      $current.city='UNKNOW' 
      $current 
    }
}

#output of $array1 modified and elements to add
$array1, $toadd
2 of 2
0

Here is a sample I created that might help. Note: I use List types instead of ArrayList ones. Also, it assumes only one possible matching PC name in the data to be updated. You'll have to alter it to update the file since it merely updates the first List variable. Let me know how it goes.

๏ปฟ[PSCustomObject]
{
[string] $pc,
[string] $name,
[string] $date,
[string] $city
}

[System.Collections.Generic.List[PSCustomObject]] $list1 = Import-Csv "C:\SOSamples\log.csv";
[System.Collections.Generic.List[PSCustomObject]] $list2 = Import-Csv "C:\SOSamples\log2.csv";
[PSCustomObject] $record = $null;
[PSCustomObject] $match = $null;

foreach($record in $list2)
{
    # NOTE: This only retrieves the FIRST MATCHING item using a CASE-INSENSITIVE comparison       
    $match = $list1 | Where { $_.pc.ToLower() -eq $record.pc.ToLower() } | Select -First 1;

    if($match -eq $null)
    {
        Write-Host "Not Found!";
        $list1.Add($record);
    }
    else 
    {
        Write-Host "Found!";
        $match.date = $record.date;
    }
}

Write-Host "--------------------------------------------------------------------"

foreach($record in $list1)
{
    Write-Host $record
}
๐ŸŒ
SharePoint Diary
sharepointdiary.com โ€บ sharepoint diary โ€บ powershell โ€บ powershell tutorials โ€บ powershell arraylist โ€“ a beginners guide!
PowerShell ArrayList - A Beginners Guide! - SharePoint Diary
September 30, 2025 - You can use the GetType() method to retrieve the data type of the element stored in the ArrayList. E.g., ... When you add items to an ArrayList using the Add() method, PowerShell returns the index where the new item was placed.
Discussions

powershell - PS: Get index in an array list - Stack Overflow
106 Get index of current item in a PowerShell loop ยท 5 retrieve array index inside Powershell foreach loop? 27 Array index from first to second last in PowerShell ยท 1 How can I find the index of a String array element using the [Array]::FindIndex method in PowerShell? 3 How to find the indexOf element in ArrayList ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Finding Arraylist Object properties
Somehow you need to tell PowerShell all of the column names; otherwise it's just going to "taste" the first object it sees and use that object's properties. A hackish way of doing that: $ColumnNames = $matrix|%{$_.psobject.Properties}|% Name |select -Unique $Matrix | Select-Object $ColumnNames Other options: collect the unique list of names in the loop, and use that instead of $ColumnNames Whenever you encounter a name that you haven't seen before, add it as a blank NoteProperty to the first object Also: if you need to the script to run faster, two easy changes are: get rid of New-Object and Add-Member, and create ordered hashtables instead, then after you've created all the hashtables, convert them all to [pscustomobject]s. you can just create $Matrix by assigning to the output of the loop Something like: $Matrix = $Files | % { $Obj = [ordered]@{ Name = ($_.Name).Split(' ')[0] } # ... $Obj[$Line] = 'x' # ... [pscustomobject]$Obj # this will drop into $Matrix, which will end up as an array. } More on reddit.com
๐ŸŒ r/PowerShell
3
1
January 28, 2019
Powershell Array list
I am looking for help in arrays and forloop for a powershell script. I have list of domains in first array I need to reiterate get-computer command and give searchbase from my second array. The first index in domains list is the input for searchbase . We have 10 domains which I need to run ... More on forums.powershell.org
๐ŸŒ forums.powershell.org
4
0
April 27, 2020
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
๐ŸŒ
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
PS> $data = @('Zero','One','Two','Three') PS> $data.Count 4 PS> $data Zero One Two Three ยท This array has 4 items. When we call the $data variable, we see the list of our items. If it's an array of strings, then we get one line per string.
๐ŸŒ
Adam the Automator
adamtheautomator.com โ€บ powershell-array
PowerShell Arrays, ArrayLists & Collections: Best Practices
To retrieve specific items from an array or ArrayList you can use many different methods. Much like other objects in PowerShell, you can access all elements of an array by simply calling the object.
Published ย  April 22, 2025
๐ŸŒ
SPGuides
spguides.com โ€บ powershell-arraylist
PowerShell ArrayList [Create and Use]
March 26, 2025 - In this tutorial, I will explain how to create and use PowerShell ArrayList with some examples for efficient data management. As a PowerShell developer or administrator when you start using ArrayList over an Array, it will improve your scriptโ€™s performance.
๐ŸŒ
Reddit
reddit.com โ€บ r/powershell โ€บ finding arraylist object properties
r/PowerShell on Reddit: Finding Arraylist Object properties
January 28, 2019 -

I am looping on a folder of output generated by the command "net localgroup administrators". Within the output is a list of security groups that belong to the local administrator group on a PC. My goal is to gather all security groups then create a matrix of PC names and these security groups. It would look something like this:

Asset SG1 SG2 SG3 SG4 SG5 SG6
PC1 x x x
PC2 x x x x
PC3 x x

Here's what I have so far for code:

$Matrix = New-Object System.Collections.ArrayList

$Matrix | Add-Member -MemberType NoteProperty -Name "Asset" -Value "" -Force

$Files = Get-ChildItem -Recurse -Path "C:\PathToFiles\"

$Files | % {
    # Create a new object that'll get added to the Matrix arraylist.
    $Obj = New-Object System.Object
    
    $Obj | Add-Member -MemberType NoteProperty -Name "Asset" -Value ($_.Name).Split(' ')[0]

    # Get the name of the asset from the filename.
    # $Asset = ($_.Name).Split(' ')[0]

    $Extract = $false   
    $Lines = Get-Content -Path $_.FullName
    
    Foreach ($Line in $Lines) {
        If ($Extract -eq $false) {
            If ($Line -match "-{2,}") {
                $Extract = $true
            }
        }
        Elseif ($Line -eq "The command completed successfully.") {
            $Extract = $false
        }
        Else {
            $Obj | Add-Member -MemberType NoteProperty -Name $Line -Value "x"
        }        
    }
    $Matrix += $Obj
}
$Matrix | ft -AutoSize

I'm not sure if it's the way I'm creating the Matrix array or something else entirely but outputting $Matrix to the screen using format-table or exporting to CSV I'm missing several columns. Doing $Matrix[0] and $Matrix[1] shows me all the data I expect...

PS C:\Scripting> $Matrix[0] | ft

Asset Airport_Admin COSAdmin D1LogRythm Services D2Domain Admins D2IT-WorkstationAdmins MJ
Machine 1 x x x x x x

PS C:\Scripting> $Matrix[1] | ft

Asset D3IT-WorkstationAdmins D1LogRythm Services D2CSPD-PowerUsers-Systems D2Domain Admins D2IT-WorkstationAdmins D2MJRCRIME MJ
Machine 2 x x x x x x x

but

$Matrix | Export-CSV "C:\Temp\Output.csv" -NTI 

Only gives me the columns present in $Matrix[0].

What silly mistake am I making? Why can't I get an output with all my columns?

Top answer
1 of 2
2
Somehow you need to tell PowerShell all of the column names; otherwise it's just going to "taste" the first object it sees and use that object's properties. A hackish way of doing that: $ColumnNames = $matrix|%{$_.psobject.Properties}|% Name |select -Unique $Matrix | Select-Object $ColumnNames Other options: collect the unique list of names in the loop, and use that instead of $ColumnNames Whenever you encounter a name that you haven't seen before, add it as a blank NoteProperty to the first object Also: if you need to the script to run faster, two easy changes are: get rid of New-Object and Add-Member, and create ordered hashtables instead, then after you've created all the hashtables, convert them all to [pscustomobject]s. you can just create $Matrix by assigning to the output of the loop Something like: $Matrix = $Files | % { $Obj = [ordered]@{ Name = ($_.Name).Split(' ')[0] } # ... $Obj[$Line] = 'x' # ... [pscustomobject]$Obj # this will drop into $Matrix, which will end up as an array. }
2 of 2
1
howdy Catalyst8487, i see that bis has pointed out the "first object props are all that show" glitch. [grin] i see two other serious problems with your code, tho. [1] using Add-Member on an empty collection on win7ps5.1 that gives a nasty red error msg about ... Add-Member : Cannot bind argument to parameter 'InputObject' because it is null. that is because there aint anything in the collection yet. [grin] if you do that AFTER adding an item to the collection ... it seems to work but adds NOTHING. do you actually get it to work? [2] using += on an arraylist converts it to an array i have been told that does not apply to win10 ... but on win7ps5.1 it always converts the arraylist to a standard array. since the entire reason for an arraylist is to avoid the slow "add is really copy", you should use .Add() to add items to an arraylist. plus, you really should consider using the generic.list instead. arraylists are deprecated ... and produce that annoying "added object index" output that pollutes the output stream if you forget to deal with it. generic.lists don't have that problem. the solution by bis - assigning the entire loop to an array - is faster than the other methods. [grin] as long as you won't need to add more items [or remove any], that is the way to go. take care, lee
๐ŸŒ
LazyAdmin
lazyadmin.nl โ€บ home โ€บ how to use powershell array โ€“ complete guide
How to Use PowerShell Array - Complete Guide โ€” LazyAdmin
January 19, 2023 - Retrieving items from the ArrayList is the same as with a normal array. You can use the index or loop through all the items in the array using one of the loop methods. There are a couple of built-in cmdlets that can be used to perform common array operations. These cmdlets make it easier to work with arrays and allow you to automate tasks. Some of the most commonly used built-in array cmdlets in PowerShell are:
Find elsewhere
๐ŸŒ
PowerShell Forums
forums.powershell.org โ€บ powershell help
Powershell Array list - PowerShell Help - PowerShell Forums
April 27, 2020 - I am looking for help in arrays and forloop for a powershell script. I have list of domains in first array I need to reiterate get-computer command and give searchbase from my second array. The first index in domains list is the input for searchbase . We have 10 domains which I need to run the get-adcomputer one by one and at the same time I need to pass searchoubase for each domain against the OU from that particular domain.
๐ŸŒ
Enterprise DNA
blog.enterprisedna.co โ€บ powershell-arraylist
Powershell ArrayList: How to Build Better Scripts โ€“ Master Data Skills + AI
When using ArrayLists, it is essential to understand the data types of the items stored within them. You can use the GetType() method in PowerShell to retrieve the data type of an object or element stored in the ArrayList.
๐ŸŒ
| How
pipe.how โ€บ new-arraylist
PowerShell Collections: ArrayList | How
January 17, 2020 - PipeHow:\Blog> $ArrayList.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True ArrayList System.Object ยท Something interesting to note here is that compared to an array, the ArrayList is not strongly typed, it can contain any object! โ€œBut wait.. Iโ€™ve created arrays that contain several types, you even talked about how PowerShell ...
๐ŸŒ
SS64
ss64.com โ€บ ps โ€บ syntax-arrays.html
Create and use PowerShell Arrays
To retrieve an element, specify its number, PowerShell automatically numbers the array elements starting at 0. ... Think of the index number as being an offset from the staring element. ... You can also combine named elements with a range, separating them with a + so $myArray[1,2+4..9] will ...
๐ŸŒ
Varonis
varonis.com โ€บ blog โ€บ powershell-array
PowerShell Array Guide: How to Use and Create
June 9, 2022 - Here, we can use the default .Net constructor to create a new ArrayList, and then using the -Add operator to add items to it. The [void] operator is there because sometimes these commands throw out strange outputs that can mess with the code. These are the most common array types in PowerShell, but ...
๐ŸŒ
Powershellexplained
powershellexplained.com โ€บ 2018-10-15-Powershell-arrays-Everything-you-wanted-to-know
Everything you wanted to know about arrays - Powershell
October 15, 2018 - The first is by mentally thinking you want the 2nd item and using an index of 2 and really getting the third item. Or by thinking that you have 4 items and you want last item, so you will just use the size to access the last item. ... PowerShell is perfectly happy to let you do that and give you exactly what item exists at index 4, $null.
๐ŸŒ
Netwrix
blog.netwrix.com โ€บ home โ€บ resources โ€บ how to use powershell arrays
How to Use PowerShell Arrays | Netwrix
December 13, 2024 - Now letโ€™s use an ArrayList to remove an item from an array. First letโ€™s create an array. $array5 = "one", "two", "three", "four", "five" $array5.gettype()
๐ŸŒ
Vexx32
vexx32.github.io โ€บ 2020 โ€บ 02 โ€บ 15 โ€บ Building-Arrays-Collections
Building Arrays and Collections in PowerShell
On the other side of things, IDictionary represents a less-strictly-ordered set of key/value pairs where a given key (often a string such as a name) is used to access the relevant item directly. The main type of collection in use in PowerShell is the humble Array, at least in terms of what is often available directly from a script. Behind the murky curtain, PowerShell actually utilises the System.Collections.ArrayList type quite heavily in its pipeline processor.
๐ŸŒ
Reddit
reddit.com โ€บ r/powershell โ€บ when to use array vs array list vs list
r/PowerShell on Reddit: When to use array vs array list vs list
April 25, 2022 - 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.
๐ŸŒ
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 ...
๐ŸŒ
Spiceworks
community.spiceworks.com โ€บ programming & development
Powershell Array list - Programming & Development - Spiceworks Community
April 27, 2020 - Hi Team, I am looking for help in arrays and forloop for a powershell script. I have list of domains in first array I need to reiterate get-computer command and give searchbase from my second array. The first index in domains list is the input for searchbase Get-ADComputer -Server [First Array] -searchbase [Second array] -filter * -Properties LastLogonDate,Name,Description,Created| Where-Object {$_.LastLogonDate -lt $date.AddDays(-60)}| Select-Object Name,DistinguishedName,Created,LastLogonDa...
๐ŸŒ
Microsoft Certified Professional Magazine
mcpmag.com โ€บ articles โ€บ 2016 โ€บ 07 โ€บ 13 โ€บ working-with-arrays.aspx
PowerShell Basics: Working with Arrays -- Microsoft Certified Professional Magazine Online
July 13, 2016 - Unfortunate, yes, but this won't deter us from having an array that is easier to add and remove items from. Enter System.Collections.ArrayList which allows us to create an empty array which we can add items to as well as remove items with little effort at all.