Last active
February 21, 2026 13:14
-
-
Save scriptingstudio/7aa1ec2b41c6105e7edb721dd51abb8e to your computer and use it in GitHub Desktop.
Yet another PS object fast comparer (2 editions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Compare-Array { | |
| [CmdletBinding()] | |
| [alias('Compare-Object2','Compare-ObjectFast')] | |
| param ( | |
| [Parameter(Position=0)] | |
| [psobject[]] $ReferenceObject, | |
| [Parameter(Position=1)] | |
| [psobject[]] $DifferenceObject, | |
| [switch] $IncludeEqual, | |
| [switch] $ExcludeDifferent | |
| ) | |
| if ($DifferenceObject.count -eq 0 -or $ReferenceObject.count -eq 0) {return} | |
| # Diff index: put the difference array into a hash table | |
| $DifHash = @{} | |
| $DifferenceObject.foreach{$DifHash[$_] = 1} | |
| # Ref index: put the reference array into a hash table | |
| $RefHash = @{} | |
| $ReferenceObject.foreach{$RefHash[$_] = 1} | |
| if ($IncludeEqual) { | |
| foreach ($item in $ReferenceObject) { | |
| if ($DifHash.ContainsKey($item)) { | |
| $DifHash.Remove($item) | |
| $RefHash.Remove($item) | |
| [pscustomobject]@{ | |
| InputObject = $item | |
| SideIndicator = '==' | |
| } | |
| } | |
| } | |
| } | |
| else { | |
| foreach ($item in $ReferenceObject) { | |
| if ($DifHash.ContainsKey($item)) { | |
| $DifHash.Remove($item) | |
| $RefHash.Remove($item) | |
| } | |
| } | |
| } | |
| if (-not $ExcludeDifferent) { | |
| foreach ($k in $RefHash.Keys) { | |
| [pscustomobject]@{ | |
| InputObject = $k | |
| SideIndicator = '<=' | |
| } | |
| } | |
| foreach ($k in $DifHash.Keys) { | |
| [pscustomobject]@{ | |
| InputObject = $k | |
| SideIndicator = '=>' | |
| } | |
| } | |
| } | |
| } # END Compare-Array | |
| function Compare-Array2 { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Position=0)] | |
| [array] $ref, | |
| [Parameter(Position=1)] | |
| [array] $dif, | |
| [switch] $IncludeEqual, | |
| [switch] $ExcludeDifferent | |
| ) | |
| if ($ref.count -eq 0 -or $dif.count -eq 0) {return} | |
| $difhash = [System.Collections.Generic.HashSet[string]]::new( | |
| [string[]]$dif, | |
| [System.StringComparer]::InvariantCultureIgnoreCase # OrdinalIgnoreCase | |
| ) | |
| $refhash = [System.Collections.Generic.HashSet[string]]::new( | |
| [string[]]$ref, | |
| [System.StringComparer]::InvariantCultureIgnoreCase # OrdinalIgnoreCase | |
| ) | |
| if ($IncludeEqual) { | |
| $ref.Where({$difhash.Contains($_)}).foreach{ | |
| [pscustomobject]@{ | |
| InputObject = $_ | |
| SideIndicator = '==' | |
| } | |
| } | |
| } | |
| if (-not $ExcludeDifferent) { | |
| $ref.Where({-not $difhash.Contains($_)}).foreach{ | |
| [pscustomobject]@{ | |
| InputObject = $_ | |
| SideIndicator = '<=' | |
| } | |
| } | |
| $dif.Where({-not $refhash.Contains($_)}).foreach{ | |
| [pscustomobject]@{ | |
| InputObject = $_ | |
| SideIndicator = '=>' | |
| } | |
| } | |
| } | |
| } # END Compare-Array2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment