I need to determine whether all elements of an array are exactly the same. I'd like this to play nice with all data-types as well, Object/String/Int/etc.
Is there an easy way to do this in PowerShell?
function Test-ArrayElementsEqual {
[CmdletBinding()]
param (
[Parameter(Mandatory,Position = 0,ValueFromPipeline)]
[object]
$Array,
)
$test = $null
foreach ($item in $Array) {
if(!$test){
$test = $item
continue
}
if($test -ne $item){
return $false
}
}
return $true
}
This is what I have now, but I have a feeling it's a broken implementation, and that there is something more elegant.
Any help would be great.
Edit:
I put together a pretty good utility function for this since last posting. It supports objects, arrays, all numerical data-types, XML, etc.
Here's the code:
https://github.com/visusys/VSYSUtility/blob/main/Public/Test-ObjectContentsAreIdentical.ps1