How can you select unique objects based on two properties of an object in powershell?

I have an array of objects with 6 properties. It looks like this:

$csvData
CURRENT DATE AND TIME : 07/10/2015 08:17:17 CST
USER NAME             : userName
COMPUTER NAME         : computerName
IP ADDRESS            : 192.168.1.1
LOGON SERVER          : logonServer
LOGON/OFF             : logon

I want to create an array of objects where the username and computer name are not duplicated. How can I get only the unique username / computer_name in powershell? Ultimately, I would like to remove all duplicates and add the “Count” property, which keeps track of the number of duplicates.

I tried:

$csvDataUnique = $csvData | Select-Object 'User Name','Computer Name' -Unique
$csvDataUnique = $csvData | sort -Property 'User Name' | Get-Unique
+7
source share
5 answers

Very similar to Matthias answer, but will have all 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' 
+10
source

You can create your own property with Select-Object. So you were already close. Try the following:

Select-Object @{Label = "Index"; Expression = {"$($_.'User Name') $($_.'Computer Name')"} } -Unique

. "", .

+3

- :

$CSVDataUnique = @{}

$csvData | foreach {
 $CSVDataUnique["$_.'User Name'\$_.'Computer Name'"]++
}
+1

Group-Object :

$Uniqs = $csvData | Group -Property "USER NAME","COMPUTER NAME" -NoElement | Select -Expand Name
+1

- Get-Unique. , - foreach.

$csvData | Sort-Object 'User Name', 'Computer Name' | Get-Unique -AsString

, Get-Unique , . , .

0

All Articles