If you know that the value occurs only once in the array, the [array]::IndexOf() method is a pretty good way to go:

$array = 'A','B','C'
$item = 'B'
$ndx = [array]::IndexOf($array, $item)

Besides being terse and to the point, if the array is very large the performance of this approach is quite a bit better than using a PowerShell cmdlet like Where-Object. Still, it will only find the first occurrence of the specified item. But you can use the other overload of IndexOf to find the next occurrence:

$ndx = [array]::IndexOf($array, $item, $ndx+1)

$ndx will be -1 if the item isn't found.

Answer from Keith Hill on Stack Overflow
๐ŸŒ
Microsoft
devblogs.microsoft.com โ€บ dev blogs โ€บ scripting blog [archived] โ€บ find the index number of a value in a powershell array
Find the Index Number of a Value in a PowerShell Array - Scripting Blog [archived]
December 7, 2011 - The command to create an array of 10 random numbers, display the contents of the array, find the index number of one item in the array, and then verify that value is shown in the following image. It is common to need to work with one half of an array at a time.
Discussions

powerpoint - Need to add and find element index value in arraylist in powershell? - Stack Overflow
@LovepreetSingh I've updated the answer, ArrayList and List both implement IndexOf(), you don't need to call [array]::IndexOf() โ€“ Mathias R. Jessen Commented Jan 29, 2016 at 9:28 ... Is it better to redirect users who attempt to perform actions they can't yet... 1 Sharepoint Powershell Online - Delete items ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 30, 2016
How to find the indexOf element in ArrayList in PowerShell - Stack Overflow
I am trying to find the index of an element from an arraylist using powershell script,however getting the below given error Method invocation failed because [System.String[]] doesn't contain a More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 1, 2016
Powershell find value in arraylist - Stack Overflow
I hope you can help me out. I work with two ArrayLists: array1 is filled with log.csv (contains header and logon-data, values of column 'pc' are unique). It's defined as ArrayList because I want t... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to find the index of an item in an array?
Hey, Scripting Guy! [explains arrays] ( http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/07/find-the-index-number-of-a-value-in-a-powershell-array.aspx ) better than I can. $array.indexof($element) More on reddit.com
๐ŸŒ r/PowerShell
3
10
June 26, 2015
๐ŸŒ
Tech thoughts
powershellone.wordpress.com โ€บ 2015 โ€บ 08 โ€บ 04 โ€บ finding-the-index-of-an-object-within-an-array-by-property-value-using-powershell
Finding the index of an object within an array by property value using PowerShell โ€“ Tech thoughts
July 25, 2018 - The problem with the $processes array is that it doesnโ€™t have an index property, but fortunately, with PowerShell itโ€™s not a problem at all to add one: $processes = Get-Process | foreach {$i=0} {$_ | Add-Member Index ($i++) -PassThru} $index = ($processes | where {$_.Name -eq "powershell"}).Index $processes[$index] This looks already better. Adding an Index property to the array makes it easy to replicate the IndexOf methodโ€™s functionality with an array of rich objects.
๐ŸŒ
SPGuides
spguides.com โ€บ powershell-arraylist
PowerShell ArrayList [Create and Use] โ€“ Learn SharePoint, Microsoft Power Platform and SPFx Tutorials โ€“ SPGuides
December 6, 2024 - ArrayList indices start at 0. Hereโ€™s an example: # Accessing elements $firstState = $statesArrayList[0] # California $secondState = $statesArrayList[1] # Texas ยท You can also use the Get() method to retrieve an element at a specific index: $statesArrayList.Get(2) # Output: Florida ยท Check ...
๐ŸŒ
LazyAdmin
lazyadmin.nl โ€บ home โ€บ how to use powershell array โ€“ complete guide
How to Use PowerShell Array - Complete Guide โ€” LazyAdmin
January 19, 2023 - Arrays in PowerShell have an index that always starts at 0. We can use this index to retrieve items from the array, simply by specifying the index number between brackets.
๐ŸŒ
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.
๐ŸŒ
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
A common programming error is created because arrays start at index 0. Off-by-one errors can be introduced in two ways. The first is by mentally thinking you want the second item and using an index of 2 and really getting the third item. Or by thinking that you have four items and you want last item, so you use the count 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.
Find elsewhere
๐ŸŒ
Powershellexplained
powershellexplained.com โ€บ 2018-10-15-Powershell-arrays-Everything-you-wanted-to-know
Everything you wanted to know about arrays - Powershell
October 15, 2018 - A common programming error is created because arrays start at index 0. An off by one error can be introduced in two very common ways. 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.
๐ŸŒ
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 - Remember that the index starts at 0, so if I wanted to look at the fourth item in the array, I am going to specify the index value of 3. Besides just finding a single item in an array, I can also look for a range of items as well just by giving the range of numbers within the brackets. Same goes for specific locations, just use a comma to separate each index that you want to view. Also a fun fact, if you want to look at the last item in an array, just specify -1 and that will get you the last item.
Top answer
1 of 2
2

try Something like this:

Copy$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.

Copy๏ปฟ[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
}
๐ŸŒ
Adam the Automator
adamtheautomator.com โ€บ powershell-array
PowerShell Arrays, ArrayLists & Collections: Best Practices
... Perhaps you need to only retrieve the first element, arrays will always have an origin of 0 representing the first element of the array. To retrieve the first element of an array, specify the index ...
Published ย  April 22, 2025
๐ŸŒ
Netwrix
blog.netwrix.com โ€บ home โ€บ resources โ€บ how to use powershell arrays
How to Use PowerShell Arrays | Netwrix
December 13, 2024 - As with most programming languages, each individual item in a PowerShell array can be accessed by an index. The index of an array starts at zero, so in an array of three items, the first item is at index 0, the second is at index 1, and the ...
๐ŸŒ
Varonis
varonis.com โ€บ blog โ€บ powershell-array
PowerShell Array Guide: How to Use and Create
June 9, 2022 - Now we have an array, we will want to access items from it. There are several ways to do this. The first is to use the index of the items in the array. As we said before, indexes start at 0, so to retrieve the first item in our array we will need to tell PowerShell to look at the item whose index is zero.
๐ŸŒ
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 ...
๐ŸŒ
SPGuides
spguides.com โ€บ powershell-array
PowerShell Array - Complete Tutorial
May 6, 2025 - Unlike standard PowerShell arrays, ArrayList is designed for efficient additions and removals. The Add method returns the index where the item was added, which you might want to suppress in some cases: $null = $customerIDs.Add(1005) # Suppresses the output ยท You can also insert an element at a specific position:
๐ŸŒ
| How
pipe.how โ€บ new-arraylist
PowerShell Collections: ArrayList | How
January 17, 2020 - Arraylists have plenty of methods you can explore by using Get-Member, and while some are shared with arrays as shown above, some are different as well. The main one to use is of course Add and something to keep in mind when using it is that it will output the index of the element it just stored.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ powershell โ€บ module โ€บ microsoft.powershell.core โ€บ about โ€บ about_arrays
about_Arrays - PowerShell | Microsoft Learn
You can also use the Remove-Item cmdlet, but assigning a value of $null is faster, especially for large arrays. Beginning in Windows PowerShell 3.0, a scalar types and collection of zero or one objects has the Count and Length properties. Also, you can use array index notation to access the value of a singleton scalar object. This feature helps you to avoid scripting errors that occur when a command that expects a collection gets fewer than two items.