Thomas Lang
Thomas Lang

Reputation: 231

Powershell: delete duplicate entry in arraylist

In my Powershellscript I read some data from a csv-File in an Arraylist.

In the second step I eliminate every line without the specific char: (.

At the third step I want to eliminate every double entries.

Example for my list:

Klein, Jürgen (Klein01); salesmanagement national 
Klein, Jürgen (Klein01); salesmanagement national 
Meyer, Gerlinde (Meyer02); accounting 
Testuser 
Admin1
Müller, Kai (Muell04); support international

I use the following script:

$Arrayusername = New-Object System.Collections.ArrayList
$NewArraylistuser = New-Object System.Collections.ArrayList
$Arrayusername = Get-Content -Path "C:\Temp\User\Userlist.csv"
for ($i=0; $i -le $Arrayusername.length; $i++)
{
    if ($Arrayusername[$i] -like "*(*")
    {
        $NewArraylistuser.Add($Arrayusername_ads[$i])
    }
    $Array_sorted = $NewArraylistuser | sort
    $Array_sorted | Get-Unique
}

But the variable $Array_sorted still has double entries. I don´t find the mistake.

Upvotes: 0

Views: 2967

Answers (1)

Paxz
Paxz

Reputation: 3046

Some Ideas how you could change your code:

  1. Use the existing Command to import .csv files with the Delimiter ;.
  2. Filter the output with Where-Object to only include Names with (.
  3. Select only unique objects with Select-Object, or if you want to sort the Object, use the Sort-Object with the same paramets.

Something like this should work:

Import-csv -Delimiter ';' -Header "Name","Position" -Path "C:\Temp\User\Userlist.csv" | Where-Object {$_.Name -like "*(*"} | Sort-Object -Unique -Property Name,Position

Upvotes: 1

Related Questions