
$files = Get-Item C:\Users\ewpmni\Desktop\NonPers\Finance*.xml
$fileCount = $files.Count
$fileNum = 0

$result = foreach($file in $files) {

    $fileNum++

    Write-Progress -Id 1 -Activity "File" -Status "$($file.Name) - Loading XML" -PercentComplete ($fileNum / $fileCount * 100)    

    $xml = [xml](Get-Content $file)

    Write-Progress -Id 1 -Activity "File" -Status "$($file.Name) - Processing invoices" -PercentComplete ($fileNum / $fileCount * 100)    

    $invoiceCount = $xml.DataExportFile.DataGroup.DataType.Finance.Invoice.Count
    $invoiceNum = 0

    foreach($invoice in $xml.DataExportFile.DataGroup.DataType.Finance.Invoice) {

        $invoiceNum++
        
        Write-Progress -ParentId 1 -Activity "Invoice" -Status "$invoiceNum / $invoiceCount" -PercentComplete ($invoiceNum / $invoiceCount * 100)

        [pscustomobject]@{
            ID = $invoice.DocumentUniqueID
            File = $file.Name
            PTO = $invoice.PTORefNumber
        }
    }

    Write-Progress -ParentId 1 -Activity "Invoice" -Completed
}


$dup = @{}

$dupIds = for($i = 0; $i -lt $result.Count; $i++) {
    $id = "{0}_{1}" -f $result[$i].ID, $result[$i].PTO

    if(-not $dup.ContainsKey($id)) {
        $dup[$id] = @( $result[$i].File )
    }
    else {
        $dup[$id] += $result[$i].File
        $id
    }
}


foreach($id in $dupIds) {
    Write-Verbose -Verbose "ID: $id"
    $dup[$id]
}