2

I have a file with the following columns: RecipientAddress and MessageId

I need to get only conntent with unique RecipientAddress. If I run:

Import-Csv $File_MsgList | select RecipientAddress | Sort-Object RecipientAddress | Get-Unique -AsString

It filters out the duplicates correctly. But If I want to get the associated MessageId as well, it is not able to remove the duplicate addresses anymore.

Import-Csv $File_MsgList | select RecipientAddress, MessageId | Sort-Object RecipientAddress | Get-Unique -AsString

How can I get both columns as well as get uniques on the first column?

3
  • What do you mean with "unique RecipientAddress"? You may share some (sanitized) sample data from your csv file. (formatted as code as well, please. ;-)) Commented Jan 29, 2021 at 1:42
  • 1
    You may use Group-Object for this purpose. You'd get unique RecipientAddresses with their associated message IDs. Commented Jan 29, 2021 at 1:45
  • 1
    Do you want to display only pairs where RecipientAddress occurs only once or do you want to display every RecipientAddress only once? For the latter, is the MessageId for every redundant RecipientAddress the same? If not, which MessageId is the desired one? Commented Jan 29, 2021 at 6:25

2 Answers 2

2

That is because Get-Unique filters each unique item (object) including all its properties.
Instead you might simply use the -Unique parameter on the Sort-Object cmdlet:

$MsgList = ConvertFrom-Csv @'
RecipientAddress, MessageId
[email protected],1
[email protected],2
[email protected],3
[email protected],4
'@

$MsgList | select RecipientAddress, MessageId | Sort-Object RecipientAddress -unique

RecipientAddress MessageId
---------------- ---------
[email protected]     1
[email protected]     2
[email protected]     4
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @iRon that was exactly what I needed.
1

You can sort by two columns and get the unique entries based on each main column entry, which here is "name":

'name,number
joe,1
joe,1
john,1
john,1' | convertfrom-csv | sort-object name,number -unique

name number
---- ------
joe  1
john 1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.