none
Powershell: Completely remove duplicates from a PSObject RRS feed

  • Question

  • Hello everybody,

    As an example I have the following little script, which creates a PSObject and fills it with data:

    $Objects = @(
        "Hans;Muster;Testweg;1"
        "Goerg;Mueller;Hauptstrasse;13"
        "Ida;Hansen;Berliner Ring;13"
        "Hugo;Klemmerle;Hindenburgstrasse;98"
        "Heinz;Klug;Ringweg;102"
        )
    
    # Create an "System.Collections.Generic.List" Objekt, in which the data should be saved later ...
    $Adresses = New-Object -TypeName System.Collections.Generic.List[Object]
    
    foreach ($Object in $Objects)
        {
        $Object = $Object.Split(";")
    
        $Adresses.Add((New-Object PSObject -Property @{ Given_Name="$($Object[0])"; Surname="$($Object[1])"; Street="$($Object[2])"; House_Number="$($Object[3])" }))
    
        }
    
    $Adresses | Sort-Object -Property Given_Name | Format-Table -Property Given_Name, Surname, Street, House_Number

    The result looks like this:

    Given_Name     Surname     Street                     House_Number
    ----------           -------         ------                     ------------
    Goerg               Mueller         Hauptstrasse           13          
    Hans                 Muster         Testweg                  1           
    Heinz                Klug             Ringweg                 102         
    Hugo                 Klemmerle    Hindenburgstrasse  98          
    Ida                   Hansen         Berliner Ring           13  

    I have the following problem:

    I want to delete all records in the PSObject array that have two or more times the same value in the "House number" column. So there should not exist a dataset which has the value 13.

    The result should look like this:

    Given_Name     Surname     Street                     House_Number
    ----------           -------         ------                     ------------
    Hans                 Muster         Testweg                  1           
    Heinz                Klug             Ringweg                 102         
    Hugo                 Klemmerle    Hindenburgstrasse  98          

    Thank you very much in advance!!!

    Wednesday, August 28, 2019 8:02 AM

All replies

  • This is a very simple issue and can be easily solved by coding it in PowerShell.

    @'
    Given_Name;Surname;Street;House_Number
    Hans;Muster;Testweg;1
    Goerg;Mueller;Hauptstrasse;13
    Ida;Hansen;Berliner Ring;13
    Hugo;Klemmerle;Hindenburgstrasse;98
    Heinz;Klug;Ringweg;102
    '@ | 
        ConvertFrom-Csv -Delimiter ';' | 
        Sort-Object -Property House_Number -Unique
    

    The key is to start with something that is technically useful.

    See this:

    PS D:\scripts> @'
    >> Given_Name;Surname;Street;House_Number
    >> Hans;Muster;Testweg;1
    >> Goerg;Mueller;Hauptstrasse;13
    >> Ida;Hansen;Berliner Ring;13
    >> Hugo;Klemmerle;Hindenburgstrasse;98
    >> Heinz;Klug;Ringweg;102
    >> '@ | ConvertFrom-Csv -Delimiter ';' | Sort -Property House_Number -Unique
    
    Given_Name Surname   Street            House_Number
    ---------- -------   ------            ------------
    Hans       Muster    Testweg           1
    Heinz      Klug      Ringweg           102
    Ida        Hansen    Berliner Ring     13
    Hugo       Klemmerle Hindenburgstrasse 98
    
    
    PS D:\scripts>


    \_(ツ)_/

    Wednesday, August 28, 2019 10:20 AM
  • Hmmm ... if I got it right the duplicates should be completely deleted ...
    $QuellDaten = @'
    "Vorname";"Nachname";"Straße";"Hausnummer"
    "Hans";"Muster";"Testweg";1
    "Goerg";"Mueller";"Hauptstrasse";13
    "Ida";"Hansen";"Berliner Ring";13
    "Hugo";"Klemmerle";"Hindenburgstrasse";98
    "Heinz";"Klug";"Ringweg";102
    '@ | ConvertFrom-Csv -Delimiter ';'
    
    $QuellDaten | 
        Group-Object -Property Hausnummer | 
            Where-Object { $_.Count -lt 2 } | 
                Select-Object -ExpandProperty Group
    ;-)  :-P

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Wednesday, August 28, 2019 10:23 AM
  • Hmmm ... if I got it right the duplicates should be completely deleted … 
    ;-)  :-P

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Same thing different language;)  Try Chinese next!


    \_(ツ)_/

    Wednesday, August 28, 2019 10:30 AM