user7189226
user7189226

Reputation:

Eliminate the duplicates from the array in powershell script

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

Upvotes: 3

Views: 11798

Answers (1)

Lakshmikant Deshpande
Lakshmikant Deshpande

Reputation: 844

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

Upvotes: 8

Related Questions