New cmdlets in CPolydorou.General v2.9.0
There is a newer version of my CPolydorou.General PowerShell module that contains two new cmdlets, Remove-Duplicate and Compare-ObjectByProperty.
Let me guide you through the usage of these cmdlets.
We'll start with Compare-ObjectByProperty. This cmdlet compares two objects using the values on their properties. Let's take the following csv file as example:
Using the Import-Csv cmdlet we are going to save it to the "data" variable for later use:
If we compare the third and the fourth object using the "-eq" operator we get that the objects are not equal which is correct since those are different objects.
However, this is not what we are looking for. Since the properties on the objects have the same values, we would like to objects to be equal. Using the Compare-ObjectByProperty we are going to compare the values of the properties of the objects:
Let's move on to the Remove-Duplicate. This cmdlet removes the duplicates objects from collections. Again, let's take for example the data in the same csv file. The third and fourth objects are the same so the latter should be removed:
Wait, that's not all, we can also compare objects using a subset of their properties. the first and the second object have the same values on the first two properties, thus should be considered duplicates. Same thing with the third and fourth object.
The module is available on the PowerShell Gallery as always.
I hope you'll find these cmdlets helpful, have fun!
Let me guide you through the usage of these cmdlets.
We'll start with Compare-ObjectByProperty. This cmdlet compares two objects using the values on their properties. Let's take the following csv file as example:
Property1 | Property2 | Property3 |
a | b | c |
a | b | d |
e | f | g |
e | f | g |
Using the Import-Csv cmdlet we are going to save it to the "data" variable for later use:
PS C:\> $data = Import-Csv -Path .\data.csv
PS C:\> $data
Property1 Property2 Property3
--------- --------- ---------
a b c
a b d
e f g
e f g
PS C:\> $data
Property1 Property2 Property3
--------- --------- ---------
a b c
a b d
e f g
e f g
If we compare the third and the fourth object using the "-eq" operator we get that the objects are not equal which is correct since those are different objects.
PS C:\> $data[2] -eq $data[3]
False
False
However, this is not what we are looking for. Since the properties on the objects have the same values, we would like to objects to be equal. Using the Compare-ObjectByProperty we are going to compare the values of the properties of the objects:
PS C:\> Compare-ObjectByProperty -ReferenceObject $data[2] -DifferenceObject $data[3]
True
True
Let's move on to the Remove-Duplicate. This cmdlet removes the duplicates objects from collections. Again, let's take for example the data in the same csv file. The third and fourth objects are the same so the latter should be removed:
PS C:\> $data | Remove-Duplicate
Property1 Property2 Property3
--------- --------- ---------
a b c
a b d
e f g
Property1 Property2 Property3
--------- --------- ---------
a b c
a b d
e f g
Wait, that's not all, we can also compare objects using a subset of their properties. the first and the second object have the same values on the first two properties, thus should be considered duplicates. Same thing with the third and fourth object.
PS C:\> $data | Remove-Duplicate -Property Property1,Property2
Property1 Property2 Property3
--------- --------- ---------
a b c
e f g
Property1 Property2 Property3
--------- --------- ---------
a b c
e f g
The module is available on the PowerShell Gallery as always.
I hope you'll find these cmdlets helpful, have fun!