Author Posts

June 1, 2018 at 10:02 am

Hi,

I need to remove rows that have duplicates PC_NAME, but I cannot seem to get it working.
I can only make it work if I select one object but I need to returns the entire rows.
I do not care what DATA is in the row, I only need to make sure that the PC_NAME is unique.

Let's say I have this data:

PC_NAME DATA
—— —-
PC_1 AJRouter
PC_1 AppIDSvc
PC_2 Server
PC_3 Oracle
PC_3 Oracle-temp

I want to return this:
PC_NAME DATA
—— —-
PC_1 AJRouter
PC_2 Server
PC_3 Oracle

This script is only returning the PC_NAME and not the entire rows:

$my_filtered_data = $pc_list | Select-Object -Property PC_NAME -Unique
$my_filtered_data

June 1, 2018 at 10:58 am

I am sure there are other ways of doing this, but using Group-Object can work.

$content = 'PC_NAME DATA
PC_1 AJRouter
PC_1 AppIDSvc
PC_2 Server
PC_3 Oracle
PC_3 Oracle-temp'

# Convert string to custom object then group objects
$group = $content | ConvertFrom-Csv -Delimiter ' ' | 
Group-Object -Property PC_Name 

# Select first object in each group
$group | ForEach-Object {$_.Group | Select-Object -First 1}

June 1, 2018 at 11:19 am

Thanks a lot! It's working great.

June 1, 2018 at 12:07 pm

import-csv unique.csv | sort -u pc_name                                                                                          

PC_NAME DATA
------- ----
PC_1    AJRouter
PC_2    Server
PC_3    Oracle