Get-ADUser with Get-Content

So i have a task to perform. i have a list of AD Users on a sheet. Column A has first name and Column B has last name. Whats the best method of using the Get-AdUser command to match the first name and last name of the user to the AD Object then Give me the SamAccountName of the object? Also if possible spit out an error log if it could not find that user or if there are duplicates? Please help. Thank you. 

Parents
No Data
Reply
  • A suggested answer is at the end of this post. If you choose not to take the TL;DR approach.

    As for this ---

    So i have a task to perform. i have a list of AD Users on a sheet. Column A has first name and Column B has last name.

    Answer: This is fine

    ---


    As for this ---

    Whats the best method of using the Get-AdUser command to match the first name and last name of the user to the AD Object then Give me the SamAccountName of the object?

    Answer: Just combine Col A and B and match that to the AD display name to get the SamAccountName.

    ---


    As for this ---

    Also, if possible, spit out an error log if it could not find that user or if there are duplicates?

    Answer: You have to code for error trapping based on what you are looking for.

    ---

    So, stepping thru each of the above, seems to indicate that you are never really fully used PowerShell, or very new to PowerShell, especially regarding AD. So, it is vital that you get ramped on it, to limit / eliminate, issues, concerns, misconceptions, frustrations, and potential major damage that can be caused if you are doing something you are unsure of, using someone else’s code (no matter where you get it from) and running it without truly understanding what it does. I say this because, what you are asking is a PowerShell 100 level question, something that is done every day, and there literally tons of examples all over the web on how to do this, even pre-written scripts that you could use as is, or tweak as needed. There are also tools built in to Windows ADDS that will write the base code for you and you can then tweak as needed. There are several examples in the PowerShell help files on your host and online.

    For example:

    Introduction to Active Directory Administrative Center Enhancements (Level 100)
    https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/get-started/adac/introduction-to-active-directory-administrative-center-enhancements--level-100-


    Learning PowerShell with Active Directory Administrative Center (PowerShell History Viewer)
    https://sid-500.com/2017/10/10/learning-powershell-with-active-directory-administrative-center-powershell-history-viewer


    Step-By-Step: Utilizing PowerShell History Viewer in Windows Server 2012 R2
    https://blogs.technet.microsoft.com/canitpro/2015/03/04/step-by-step-utilizing-powershell-history-viewer-in-windows-server-2012-r2


    Use Active Directory Administrative Center to Create PowerShell Commands in Windows Server 2012
    https://www.petri.com/use-active-directory-administrative-center-create-powershell-commands


    That being said, see my reddit post for learning this stuff.
    https://www.reddit.com/r/PowerShell/comments/bserj9/learn_powershell/eooduq9/?context=3
    https://www.reddit.com/r/PowerShell/comments/bserj9/learn_powershell/eoodxzu/?context=3

    So, in short, relative to your use case.

    Read in the file using:
    Import-Csv, not get content, since this is already columnar / table-based file. Hopefully, you have headers (which become properly names) on the file, and if not, that cmdlet allows for creating them on import.

    On the read, just combine the two columns as a single variable and use that to match the AD User display name and select the SamAccountName

    There will never be duplicate user names in ADDS. Names must be unique. If you are saying there may be duplicate names in your import file, then correct that first, so you don’t have to deal with such things.

    Error message happen by default. There are different types of errors, mostly these are terminating and non-terminating errors caused by code, or environment. You must decide how to handle each. You can get a log automatically for you process simply by using the transcript cmdlets.

    Start-Transcript
    Stop-Transcript

    So, something as simple after looking at the help files / examples for the aforementioned cmdlets:


    Start-Transcript -Path '.\ADUserCheckReport.txt'
    Import-Csv -Path 'C:\Temp\ADUserLIst.csv' |
    ForEach {
        "Processing $PSitem"
         (Get-ADUser -Filter "DisplayName -eq '$($PSItem.FirstName) $($PSItem.LastName)'").SamAccountName
    }
    Stop-Transcript

Children