$select = Select-String -Path .\Reconciliation-NonPersistent-AX-510-20151119-H.xml -Pattern '(.*?)<\/OrderID>' $matches = foreach($s in $select) { $s.Matches.Groups[1].Value } $sorted = $matches | Sort-Object $unique = $matches | Sort-Object -Unique $lastIndex = 0 $indexes = for($i = 0; $i -lt $unique.Count; $i++) { if($i % 100) { Write-Progress -Activity 'Scanning' -Status "$i / $($unique.Count)" -PercentComplete ($i / $unique.Count * 100) } $lastIndex = GetIndexOf -uniqueIndex $i -startIndex $lastIndex Write-Output ([pscustomobject]@{ Index = $lastIndex Value = $unique[$i] Count = (GetIndexOf -uniqueIndex ($i+1) -startIndex $lastIndex) - $lastIndex }) } function GetIndexOf($uniqueIndex, $startIndex) { for($i = $startIndex; $i -lt $sorted.Count; $i++) { if($sorted[$i] -eq $unique[$uniqueIndex]) { return $i; } } return -1 }