0

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.

1
  • Your code needs one more } and that might affect the outcome. When I test with your data, Get-unique removes the duplicate name. Your code could be $result = Get-Content -Path "C:\Temp\User\Userlist.csv" | Where-Object { $_ -like '*(*' } | Sort-Object -Unique Commented Mar 5, 2019 at 6:53

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. I have tested your code an it worked well. When I use the "-Encoding UTF8" parameter I get the following Errormessage "No Parameter found corresponding to the parameter "Encoding"
Hey @ThomasLang, I'm glad that I could help. The -Encoding Parameter only exists since Powershell v3. If you are still using v2, is will throw an error (like the one you mentioned). You may want to upgrade your Powershell Version in the near future ;). Also, if the answer solved your problem, you can mark the answer as correct (stackoverflow.com/help/someone-answers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.