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 Reply
  • You did not say in your original post that you wanted to export this to a file.

    So, FYI, when you post to Q&A site, you must provide all of what you've tried, done, errors, input and expected output, or you leave people to guess, and end up in far longer threads then needed.

    This error ...

    Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.

    ... specifically, says, that nothing is coming back from your Csv file or your code.

    How is this 'EmailAddress' shown in the file?
    Meaning, emailaddres or
    emailaddress@domain.com?

    You need to be specific for what you ask for (in your file or in your code), PowerShell will not try an figure it out for you.


    As for this ...

    And im trying to translate over 600 email addresses to the users samaccountname

    ... I don't understand what you mean by it.

    The default for ADDS and Exchange is that the user default SamAccountName, is there email alias. So, they are the same by default.
    If you run ...

    Get-Mailbox

    ... on your Exchange server (or via an implicit / explicit PowerShell Remoing session), you see that name in the ALias property.
    Unless your ADDS folks are doing something unique, there should be no need for 'translation' of any sort.
    ALso, by default, the UPN is the users email address.

    Lastly, not all properties are returned using the default cmdlet, there will be cases where you must specifically ask to see and use it. For example:


    # Get the first user fromm AD and look at the default properties returned.
    (Get-ADUser -Filter *)[0] | Get-Member

    <#
       TypeName: Microsoft.ActiveDirectory.Management.ADUser

    Name              MemberType            Definition                                                                                                                    
    ----              ----------            ----------                                                                                                                    
    Contains          Method                bool Contains(string propertyName)                                                                                            
    Equals            Method                bool Equals(System.Object obj)                                                                                                
    GetEnumerator     Method                System.Collections.IDictionaryEnumerator GetEnumerator()                                                                      
    GetHashCode       Method                int GetHashCode()                                                                                                             
    GetType           Method                type GetType()                                                                                                                
    ToString          Method                string ToString()                                                                                                             
    Item              ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string propertyName) {get;}                               
    DistinguishedName Property              System.String DistinguishedName {get;set;}                                                                                    
    Enabled           Property              System.Boolean Enabled {get;set;}                                                                                             
    GivenName         Property              System.String GivenName {get;set;}                                                                                            
    Name              Property              System.String Name {get;}                                                                                                     
    ObjectClass       Property              System.String ObjectClass {get;set;}                                                                                          
    ObjectGUID        Property              System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] ObjectGUID {ge...
    SamAccountName    Property              System.String SamAccountName {get;set;}                                                                                       
    SID               Property              System.Security.Principal.SecurityIdentifier SID {get;set;}                                                                   
    Surname           Property              System.String Surname {get;set;}                                                                                              
    UserPrincipalName Property              System.String UserPrincipalName {get;set;}                                                                                    
    #>

    # Get the first user from AD and look at the default and additional properties returned.
    (Get-ADUser -Filter '*' -Properties emailaddress)[0] | Get-Member
    <#
       TypeName: Microsoft.ActiveDirectory.Management.ADUser

    Name              MemberType            Definition                                                                                                                    
    ----              ----------            ----------                                                                                                                    
    Contains          Method                bool Contains(string propertyName)                                                                                            
    Equals            Method                bool Equals(System.Object obj)                                                                                                
    GetEnumerator     Method                System.Collections.IDictionaryEnumerator GetEnumerator()                                                                      
    GetHashCode       Method                int GetHashCode()                                                                                                             
    GetType           Method                type GetType()                                                                                                                
    ToString          Method                string ToString()                                                                                                             
    Item              ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string propertyName) {get;}                               
    DistinguishedName Property              System.String DistinguishedName {get;set;}                                                                                    
    EmailAddress      Property              System.String EmailAddress {get;set;}                                                                                         
    Enabled           Property              System.Boolean Enabled {get;set;}                                                                                             
    GivenName         Property              System.String GivenName {get;set;}                                                                                            
    Name              Property              System.String Name {get;}                                                                                                     
    ObjectClass       Property              System.String ObjectClass {get;set;}                                                                                          
    ObjectGUID        Property              System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] ObjectGUID {ge...
    SamAccountName    Property              System.String SamAccountName {get;set;}                                                                                       
    SID               Property              System.Security.Principal.SecurityIdentifier SID {get;set;}                                                                   
    Surname           Property              System.String Surname {get;set;}                                                                                              
    UserPrincipalName Property              System.String UserPrincipalName {get;set;}                                                                                    
    #>


    # Get the first user from AD and look at the all properties returned.
    (Get-ADUser -Filter '*' -Properties '*' )[0] | Get-Member
    <#
       TypeName: Microsoft.ActiveDirectory.Management.ADUser

    Name                                 MemberType            Definition                                                                                                 
    ----                                 ----------            ----------                                                                                                 
    ...
    Department                           Property              System.String Department {get;set;}                                                                        
    Description                          Property              System.String Description {get;set;}                                                                       
    directReports                        Property              Microsoft.ActiveDirectory.Management.ADPropertyValueCollection directReports {get;}                        
    DisplayName                          Property              System.String DisplayName {get;set;}                                                                       
    DistinguishedName                    Property              System.String DistinguishedName {get;set;}                                                                 
    Division                             Property              System.String Division {get;set;}                                                                          
    DoesNotRequirePreAuth                Property              System.Boolean DoesNotRequirePreAuth {get;set;}                                                            
    dSCorePropagationData                Property              Microsoft.ActiveDirectory.Management.ADPropertyValueCollection dSCorePropagationData {get;}                
    EmailAddress                         Property              System.String EmailAddress {get;set;}                                                                      
    EmployeeID                           Property              System.String EmployeeID {get;set;}                                                                        
    EmployeeNumber                       Property              System.String EmployeeNumber {get;set;}                                                                    
    Enabled                              Property              System.Boolean Enabled {get;set;}                                                                          
    ...
    mail                                 Property              System.String mail {get;set;}                                                                              
    mailNickname                         Property              System.String mailNickname {get;set;}                                                                      
    ...
    SamAccountName                       Property              System.String SamAccountName {get;set;}                                                                    
    ....
    #>


    If your files' email column is this way ... emailaddress@domain.com then this should work as expected.

    Import-Csv -Path 'C:\Temp\ADUserLIst.csv' |
    ForEach {
        "Processing $PSitem"
         (Get-ADUser -Filter "EmailAddress -eq '$($PSItem.emailaddress)'").SamAccountName
    }


    If you files email column is this way ... emailaddress then you have to add the domain name in code, because that is not an email address, it is a user/email alias name.
    It's easier just to fix this in the file, or you end up with stuff like this...

    Import-Csv -Path 'C:\Temp\ADUserLIst.csv' |
    ForEach {
        "Processing $PSitem"
         (Get-ADUser -Filter "EmailAddress -eq '$($PSItem.emailaddress + "@$env:USERDNSDOMAIN")'").SamAccountName
    }

    Sure, it works, but, well, you know.

    FYI...

    I know each f what I provided works, because I did them in one of my labs as part of the response.

    As far as the export is concerned, it's only one column, so, no real need for Export -Csv use, since there is noting to comma separate. Also, you have that command in the wrong place, as you want to write to the file as each record is processed inside the ForLoop,, so each record is written, not after it is done.

    Import-Csv -Path 'C:\Temp\ADUserLIst.csv' |
    ForEach {
         (Get-ADUser -Filter "EmailAddress -eq '$($PSItem.emailaddress)'").SamAccountName |
         Out-File -FilePath 'c:\temp\UserEmailReport.txt' -Append
    }

    If you want to do it after, then you have to collect all that first, then output.

    $UserEmailArray = @()
    Import-Csv -Path 'C:\Temp\ADUserLIst.csv' |
    ForEach {
         $UserEmailArray += $((Get-ADUser -Filter "EmailAddress -eq '$($PSItem.emailaddress)'").SamAccountName  )
    }
    $UserEmailArray | Out-File -FilePath 'c:\temp\UserEmailReport.txt'

    https://powershellexplained.com/2018-10-15-Powershell-arrays-Everything-you-wanted-to-know

Children