Script for searching AD and comparing Givenname.Surname to their Username and exporting any incorrect usernames to a CSV file.

Christopher Bliss 21 Reputation points
2022-02-21T02:38:09.707+00:00

Hello!

I am fairly new to the scripting world and am currently having trouble building a script that can search AD and compare Givenname.Surname to their Username and exporting any incorrect usernames to a CSV file. I appreciate any help in advance. I already have a script that pulls names and compares them, but I am having trouble getting it to list what usernames are the incorrect ones. Thanks for the future help!

Chris

Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-02-21T15:36:08.077+00:00

    It's not clear if you want ALL users or just the ones that don't match. This code will show you all of them with a TRUE/FALSE column indicating a match or mismatch:

    Get-ADUser -Filter * |
        ForEach-Object {
            [PSCustomObject]@{
                givenname = $_.givenname
                surname = $_.surname
                samaccountname = $_.samaccountname
                match = (("{0}.{1}" -f $_.givenname, $_.surname) -eq $_.samaccountname)
            }
        } | Export-Csv C:\Users\iceni\Desktop\BOOK.csv -NoTypeInformation
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.