Repository: microsoft/CSS-Exchange
File: M365/MDO/ResendFailedMail.ps1 (PowerShell)

Code before:
        exit
    }
    if (($graphConnection.TenantId) -ne ($exoConnection.TenantId) ) {
        Write-Host "`nThe Tenant Id from Graph and EXO are different. Please use the same tenant." -ForegroundColor Red
        exit
    }
}

if ($PsCmdlet.ParameterSetName -eq 'ResendCopyFailedDays') {
    $StartDate = (Get-Date).AddDays(-$Days)
    Write-Verbose "StartDate: $StartDate"
    $EndDate = Get-Date
    Write-Verbose "EndDate: $EndDate"
}

$traceParams = @{
    Status = "Failed"
}

if ($StartDate) { $traceParams["StartDate"] = $StartDate }
if ($EndDate) { $traceParams["EndDate"] = $EndDate }
if ($RecipientAddress ) { $traceParams["RecipientAddress"] = $RecipientAddress }
if ($SenderAddress) { $traceParams["SenderAddress"] = $SenderAddress }
if ($MessageId) { $traceParams["MessageId"] = $MessageId }

$failedMessages = New-Object System.Collections.Generic.List[object]

try {
    $failedMessages += Get-MessageTrace @traceParams
} catch {
    Write-Host "Error: $_.Exception.Message" -ForegroundColor Red
    exit
}

if ($failedMessages.count -ge 1000) {
    Write-Host "We get more than 1000 messages, please limit your search." -ForegroundColor Red
    exit
}

if (-not $IncludeDuplicates) {
    $uniqueMessages = New-Object System.Collections.Generic.List[object]
    $uniqueMessages += $failedMessages | Sort-Object MessageId -Unique
    $failedMessages = $uniqueMessages
}

$verifiedAcceptedSenderMessages = New-Object System.Collections.Generic.List[object]
$count = 0
$totalMessages = $failedMessages.Count
foreach ($failedMessage in $failedMessages) {
    $count++
    Write-Progress -Activity "Checking Progress" -Status "$count of $totalMessages" -PercentComplete ($count / $totalMessages * 100) -CurrentOperation "Checking message $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"

Review comment (suggestion):
For all the `New-Object System.Collections.Generic.List[object]` you are creating a list, but then doing the action of `+=` to attempt to add to the list. However, this process in PowerShell will convert the variable to an `Array` which is a static list vs a dynamic one.

You actually only need to create a single list in the 3 locations that you have attempted to use this. 

Code after:
        exit
    }
    if (($graphConnection.TenantId) -ne ($exoConnection.TenantId) ) {
        Write-Host "`nThe Tenant Id from Graph and EXO are different. Please use the same tenant." -ForegroundColor Red
        exit
    }
}

if ($PsCmdlet.ParameterSetName -eq 'ResendCopyFailedDays') {
    $StartDate = (Get-Date).AddDays(-$Days)
    Write-Verbose "StartDate: $StartDate"
    $EndDate = Get-Date
    Write-Verbose "EndDate: $EndDate"
}

$traceParams = @{
    Status = "Failed"
}

if ($StartDate) { $traceParams["StartDate"] = $StartDate }
if ($EndDate) { $traceParams["EndDate"] = $EndDate }
if ($RecipientAddress ) { $traceParams["RecipientAddress"] = $RecipientAddress }
if ($SenderAddress) { $traceParams["SenderAddress"] = $SenderAddress }
if ($MessageId) { $traceParams["MessageId"] = $MessageId }

try {
    [array]$failedMessages = Get-MessageTrace @traceParams -ErrorAction Stop
} catch {
    Write-Host "Error: $_.Exception.Message" -ForegroundColor Red
    exit
}

if ($failedMessages.count -ge 1000) {
    Write-Host "We get more than 1000 messages, please limit your search." -ForegroundColor Red
    exit
}

if (-not $IncludeDuplicates) {
    [array]$failedMessages = $failedMessages | Sort-Object MessageId -Unique
}

$verifiedAcceptedSenderMessages = New-Object System.Collections.Generic.List[object]
$count = 0
$totalMessages = $failedMessages.Count
foreach ($failedMessage in $failedMessages) {
    $count++
    Write-Progress -Activity "Checking Progress" -Status "$count of $totalMessages" -PercentComplete ($count / $totalMessages * 100) -CurrentOperation "Checking message $($failedMessage.MessageId) - Subject: $($failedMessage.Subject)"
    Write-Verbose "Checking $($failedMessage.SenderAddress)"
    $tempAddress = $null
    if ($recipientCache.ContainsKey($failedMessage.SenderAddress)) {
        Write-Verbose "RecipientAddress  $($failedMessage.SenderAddress) found in cache"