How can you select unique objects based on two properties of an object in powershell?
Very similar to Mathias' answer, but will have all of the columns in the output:
$csvDataUnique = $csvData | Group-Object 'User Name','Computer Name' | %{ $_.Group | Select 'User Name','Computer Name' -First 1} | Sort 'User Name','Computer Name'
You can create a custom property with your Select-Object
. So you were pretty close already. Try this:
Select-Object @{Label = "Index"; Expression = {"$($_.'User Name') $($_.'Computer Name')"} } -Unique
It basically combines the two fields into a single string and sorts unique on that. I called the field "Index" but it could be called anything.
I usually resort to using a hash table, and joined property values for the keys for that kind of task:
$CSVDataUnique = @{}$csvData | foreach { $CSVDataUnique["$_.'User Name'\$_.'Computer Name'"]++}