Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Objects In Single PowerShell Array

Tags:

powershell

I've searched but I can only find posts comparing two different arrays in PowerShell. What I'm trying to do is compare the contents within a single array to see if everything inside is equal (e.g.; 2,2,2,2 = true; 2,2,2,3 = false). Does anyone have any ideas how this might be accomplished? Thanks.

like image 417
mada2885 Avatar asked Mar 10 '21 21:03

mada2885


People also ask

How do I compare two arrays of objects in PowerShell?

You can also use PowerShell to compare arrays using the Compare-Object cmdlet. This cmdlet takes a reference object and a difference object and returns a side indicator indicating which elements are and are not in either array. You can see below that the Compare-Object cmdlet allows you to compare both arrays at once.

What is SideIndicator in PowerShell?

SideIndicator shows which input object the output belongs to. The following examples shows the different output types. PowerShell Copy.

How to use compare-object in PowerShell?

How to use Compare-Object in PowerShell? Compare-Object command in PowerShell is used to compare two objects. Objects can be a variable content, two files, strings, etc. This cmdlet uses few syntaxes to show the difference between objects which is called side indicators.

How to compare arrays with PowerShell?

One of the easiest ways to compare arrays with PowerShell is if you have two arrays only containing strings. When you find yourself in this position, you’ve got a few different ways to compare strings in the arrays. The -contains operator is a PowerShell operator that allows you to check to see if an object is in a collection.

How to compare two array objects and find the unique elements?

Do you want to compare two array objects and find the unique elements within the second array? Add a proper trigger, here I use Flow Button trigger. Add a Variables-> "Initialize variable 3" action, Name set to UniqueEmails, Type set to Array and Value set to empty. Add a "Apply to each" action, input parameter set to Array2 variable.

How do I compare two text files in PowerShell?

Open PowerShell, and run the below commands to get the content ( Get-Content) of the specified files into variables ( $file1 and $file2 ). Replace the path below with your chosen files’ path to compare. # Grab the content of the text files into variables $file1 = Get-Content C:\Temp\File1.txt $file2 = Get-Content C:\Temp\File2.txt


Video Answer


2 Answers

You can make use of Get-Unique

$array = @(2, 2, 2, 2)

if (($array | Get-Unique).Count -gt 1) {
    Write-Host "some odd ones"
} else {
    Write-Host "all the same"
}

It will count how many unique items exist in the array

We pass that result to be evaluated by the if statement

If there is more than one unique result from Get-Unique, we know that all elements are not equal

Check the SS64 page

like image 91
Bassie Avatar answered Oct 16 '22 07:10

Bassie


You can try something like this which iterates through element and compares it to the last to make sure that they are all equal.

You could make a function out of this logic that takes an array as a parameter and returns either $true or $false so that you can reuse. You may also be able to improve upon it by breaking out of the loop if you find something that is not equal.

$arr = (2,2,2,3)

$notEqual = $false;

for ($i = 1; $i -lt $arr.Length; $i++)
{
  if ($arr[$i] -ne $arr[$i-1]) {$notEqual = $true}
}

if ($notEqual)
{
    Write-Host "Array elements not equal"
}
else
{
    Write-Host "Array members are qual"
}
like image 21
Alex Riveron Avatar answered Oct 16 '22 06:10

Alex Riveron