3

enter image description hereI imported a CSV which has two groups and group members displayed before them. there are duplicate members in both the groups like : Group A:

John Harry Berke

  1. John
  2. Tom
  3. Hilton

Group B:

  1. John
  2. Louise

(John is repeated), So I did the below code to add all the group members in a array and then print the unique from them but it does not works.The array has repeated names

    $Global:grpmember = @()
    $CSV=Import-Csv -Path "C:\Temp\LicenseCount\Names.csv" -delimiter ","
    $csv | % {
    $grpmember+= $_.GroupMembers
    Write-Host "I am executed"
    }
    $grpmember | Get-Unique -AsString

enter image description here

3
  • Your question doesn't compute as .CSV files do not support line breaks (as you can see from your paste: all names are joined with a single space) and I am sure that if you import this .CSV back into your application (or Excel) you will find that that all the names are joined with a single space. How do you want separate the names if you can't make a difference between the space first/last name and each full name? I think that you need to forget about .CVS files and fallback on e.g. the PSExcel module. Commented Nov 2, 2017 at 12:45
  • Any work around ? I have to combine the members of two groups(part of 1 application) and then extract unique members from them to count the licenses.... because of repeating names of the same user in different groups , the count is also duplicating for the license Commented Nov 2, 2017 at 13:25
  • That depends whether the source application(where the screenshot comes from) can export to something else then CSV files or can e.g. escape newlines with something like ... `r`n... or ...\r\n... Commented Nov 2, 2017 at 14:55

1 Answer 1

8

Get-Unique should work here. But it needs a sorted array, and your input is not sorted.

There are some other options. Suppose you have an array, say:

$array = @('a', 'b', 'c', 'a')

You can use select -uniq or sort -uniq in this case, to remove duplicates from the above array.

$array= $array | select -uniq
# or
$array= $array | sort -uniq
Sign up to request clarification or add additional context in comments.

10 Comments

$Global:grpmember = @() $CSV=Import-Csv -Path "C:\Temp\LicenseCount\Names.csv" -delimiter "," | Sort -Unique $csv | % { $grpmember+= $_.GroupMembers Write-Host "I am executed" } $grpmember|Get-Unique -AsString
This only runs for 1 group and ignores other groups in the csv
is your csv malformed ? What about case sensitivity ?
Can you paste the csv file in the question ? I didn't understand the structure of the csv.
I have the 'Application name' , 'Group Name', 'Group Users' as heading.The The application name is same, but there are 2 groups - A01 admin,A01 users. Then the group members are displayed before them
|