Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate the duplicates from the array in powershell script

Tags:

powershell

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


1 Answers

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
like image 88
Lakshmikant Deshpande Avatar answered Sep 22 '26 08:09

Lakshmikant Deshpande



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!