r/PowerShell • u/JohnC53 • Jan 15 '26
Solved Find duplicates in array - But ADD properties to them
Example data:
| DeviceName | AssignedUser |
|---|---|
| Device01 | John Doe |
| Device02 | Biggy Smalls |
| Device03 | Biggy Smalls |
Ideal Output:
| DeviceName | AssignedUser | MultipleDevices |
|---|---|---|
| Device01 | John Doe | |
| Device02 | Biggy Smalls | TRUE |
| Device03 | Biggy Smalls | TRUE |
I can think of some rudimentary methods, but I just know it wouldn't be ideal. My dataset has about 40K rows. Looking for an efficient route. But even if it takes 30 minutes, so be it.
19
Upvotes
6
u/OddElder Jan 15 '26
2
u/krzydoug Jan 15 '26
What methods have you tried? You'll get a lot more help if you show your work. Currently it reads like "do this for me" What are trying to accomplish, exactly? What properties? Where will they come from? Help us help you.
1
u/ankokudaishogun Jan 15 '26
Well, to start we'd need to know how you get those data. CSV? JSON? DB call?
Are they simple PSCustomObjects? Hashtables? Something else?
The duplicate values must be searched for every property or only in the
AssignedUserproperty?Also, rudimentary methods aren't necessarily non-ideal.
Powershell has a lot of efficiency black magic under the hood.
What have you tried?
2
u/y_Sensei Jan 15 '26
If you want to modify the duplicate elements in your array on-the-fly, without using an additional data structure, you could utilize LINQ, specifically its
Aggregate()method, as in:1
u/OPconfused Jan 15 '26
I created a small custom collection a while back that did this, so that whenever you ran .Add on it, it checked if a duplicate was already in the collection, and if yes, it updated a column on the existing entry in the collection instead.
1
u/dodexahedron Jan 15 '26
ADD properties are in abundance, here. Want some of mine?
1
u/Future-Remote-4630 Jan 15 '26
I think the hash table approach is overkill here. A normal groupinfo object gives you everything you need for this.
1
u/fatherjack9999 Jan 16 '26
Try this compare-stringset