locked
Displayname not working & Improvement on my script? RRS feed

  • Question

  • Hi all,

    Since a couple of weeks i'm learning Powershell, and i have come up with some scripts which are actually working.

    For this script I have 2 questions: First one: For some reason the result of DisplayName are empty for each row. Probably my head is playing tricks with me, but i cannot see the problem.

    Second: This script is rather long with multiple steps: Do something, export to CSV -> Import CSV, Do something, export to CSV -> Import CSV, Do somethin, export to CSV.

    Is there a better way to (re)write this, or is this just fine? It takes some time to process, but it works :-).

    My code:

    # Checks if H:\Powershell exists and create if it not exists
    $dir = "H:\Powershell"
    if(!(test-path $dir))
    {
        New-Item -ItemType Directory -force -Path $dir
    }
    
    
    # Lookup Duplicate ADusers
    $users = Get-ADUser -Filter {(SN -like "a*")} -Properties SN
    $filteredOU = $users | Where-Object -FilterScript { 
        ($_.distinguishedname -notlike '*LSA*')            -and
        ($_.distinguishedname -notlike '*Removed*')        -and
        ($_.distinguishedname -notlike '*Disabled*')       -and
        ($_.distinguishedname -notlike '*Administration*') -and
        ($_.distinguishedname -notlike '*WSA*')            -and
        ($_.distinguishedname -notlike '*CSA*')            -and
        ($_.distinguishedname -notlike '*DSA*')            -and
        ($_.distinguishedname -notlike '*NPA*') 
    }
    $idLookup = $filteredOU | ForEach-Object { $idLookup = @{} } { if($_.Surname) { $idLookup[$_.Surname] += 1 } } { $idLookup }
    $filteredUsers = $filteredOU | Where-Object { if($_.Surname) { $idLookup[$_.Surname] -gt 1 } }
    $filteredUsers | Select-Object -Property SN, Givenname, Name, DisplayName, DistinguishedName | Sort-object -property SN |
    Export-Csv -Path H:\Test\Duplicate_ADusers.csv -NoTypeInformation
    
    # Concatenate by import newly created CSV
    $Date = get-date -Format 'ddMMMMyyyy'
    $objs =@();
    $output = Import-csv -Path "H:\Test\Duplicate_ADusers.csv" | ForEach { 
    $Object = New-Object PSObject -Property @{            
            SN                 = $_.SN
            GivenName          = $_.GivenName
            Name               = $_.Name
            DisplayName        = $_.DisplayName
            DistinguishedName  = $_.DistinguishedName
            Combined           = [String]::Concat($_.SN, $_.GivenName)
            "Created on $Date" = ""
        } | Select SN, GivenName, Name, DisplayName, DistinguishedName, Combined, "Created on $Date"  
        $objs += $Object;
    } 
    $objs 
    $objs | Export-CSv H:\Test\Duplicate_ADusers.csv -NoTypeInformation 
    
    # Filter out duplicates after Concatenate
    Import-Csv H:\Test\Duplicate_ADusers.csv |
    Group-Object -Property Combined |
    Where-Object { $_.count -ge 2 } |
    Foreach-Object { $_.Group } |
    Select SN, GivenName, Name, DisplayName, DistinguishedName, Combined, "Created on $Date" |
    Export-Csv -Path H:\Test\Duplicate_ADusersTEMP.csv -NoTypeInformation

    Thanks in advance

    Thursday, May 2, 2019 8:00 AM

Answers

  • Hi Mikkelback,

    You will have to add the DisplayName to the properties when retrieving the Users, or it will be empty.

    Modify:

    $users = Get-ADUser -Filter {(SN -like "a*")} -Properties SN

    To: 

    $users = Get-ADUser -Filter {(SN -like "a*")} -Properties SN, DisplayName
    • Marked as answer by Mikkelback Thursday, May 2, 2019 9:50 AM
    Thursday, May 2, 2019 9:26 AM
  • The Get-ADUser cmdlet returns a default set of property values. DisplayName is not one of them. If you need additional properties, you have to specify them.

    Refer to the documentation for additional information: https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-aduser?view=win10-ps#outputs

    If you have multiple questions it is advised to post these separately. Please feel free to create a new post regarding your other question.

    • Marked as answer by Mikkelback Thursday, May 2, 2019 12:08 PM
    Thursday, May 2, 2019 11:01 AM

All replies

  • Edit: I have replaced DisplayName with SamAccountName and now this column gets filled with values.

    When replace it again to DisplayName -> Empty.

    Thursday, May 2, 2019 8:14 AM
  • Can you give a brief explanation of what you are trying to achieve with this script please?
    Thursday, May 2, 2019 9:04 AM
  • Sure: I want a list of all duplicate users in AD (or looks like duplicate users, based on name, and then manually check whether a user has 2 accounts or it concerns 2 users with the same name).

    I filter out some OU's so Admin accounts etc are out of scope.

    Thursday, May 2, 2019 9:15 AM
  • Hi Mikkelback,

    You will have to add the DisplayName to the properties when retrieving the Users, or it will be empty.

    Modify:

    $users = Get-ADUser -Filter {(SN -like "a*")} -Properties SN

    To: 

    $users = Get-ADUser -Filter {(SN -like "a*")} -Properties SN, DisplayName
    • Marked as answer by Mikkelback Thursday, May 2, 2019 9:50 AM
    Thursday, May 2, 2019 9:26 AM
  • Omg, really? It works!! That was a quick fix :-) But why do i have to specify Displayname there, but i don't need to do that for the other properties (as Name, GivenName, DistinguishedName etc)?

    So the DisplayName issue is solved, thanks! But now the 2nd question if it is better to rewrite this code for simplicity or speed, less steps etc.

    Thursday, May 2, 2019 9:53 AM
  • The Get-ADUser cmdlet returns a default set of property values. DisplayName is not one of them. If you need additional properties, you have to specify them.

    Refer to the documentation for additional information: https://docs.microsoft.com/en-us/powershell/module/addsadministration/get-aduser?view=win10-ps#outputs

    If you have multiple questions it is advised to post these separately. Please feel free to create a new post regarding your other question.

    • Marked as answer by Mikkelback Thursday, May 2, 2019 12:08 PM
    Thursday, May 2, 2019 11:01 AM
  • Thanks for your info and advice!

    And i will create a new topic for my other question!

    Thursday, May 2, 2019 12:09 PM