Get-ADUser -Filter

Roger Roger 7,181 Reputation points
2022-11-29T22:01:44.58+00:00

Hi All

i have users primary smtp address in csv file. when i import to the csv file and fetch the below information i am getting error. experts correct me the syntax
my csv file has the below information
user1@Company portal .com
user2@Company portal .com

$emailAddresses = Get-Content C:\input\list.csv   
ForEach ($emailAddress in $emailAddresses) {      
Get-ADUser -Filter { mail -like $emailAddress } -Properties * | Select DisplayName,GivenName,Surname,SamAccountName,EmailAddress,Userprincipalname,title,Office,description,DepartmentNumber,employeeNumber,employeeType,manager |Select Surname,GivenName,DisplayName,SamAccountName,EmailAddress,Userprincipalname,title,Office,employeeNumber,employeeType,description,co,personalTitle,@{Name='DepartmentNumber';Expression={[string]::join(";", $($_.DepartmentNumber))}},@{name='Manager';expression={$_.manager -match '(?<=CN=).+?(?=,)'|Out-Null;$Matches.Values}}  | Export-csv C:\temp\output.csv -Notypeinformation -Append   
}  
  
Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-11-30T03:22:37.65+00:00

    Syntactically, I don't think there are any errors. But, if you unwind that wad of code you get this:

    $emailAddresses = Get-Content C:\input\list.csv   
    
    ForEach ($emailAddress in $emailAddresses) {      
        Get-ADUser -Filter { mail -like $emailAddress } -Properties * |   
            Select-Object   DisplayName,GivenName,Surname,SamAccountName,EmailAddress,Userprincipalname,  
                            title,Office,description,DepartmentNumber,employeeNumber,employeeType,manager |  
                Select-Object   Surname,GivenName,DisplayName,SamAccountName,EmailAddress,Userprincipalname,  
                                title,Office,employeeNumber,employeeType,description,co,personalTitle,  
                                @{Name='DepartmentNumber';Expression={[string]::join(";", $($_.DepartmentNumber))}},  
                                @{name='Manager';expression={$_.manager -match '(?<=CN=).+?(?=,)' | Out-Null; $Matches.Values}}  |   
                    Export-csv C:\temp\output.csv -Notypeinformation -Append   
    }  
    

    Which would be easier to decipher. I don't understand why there are two Select-Object cmdlets, though.

    Also, the regex to extract the manager's CN value may surprise you if there is an escaped comma in the CN (e.g., "CN=Jones\, John,".

    It would also help if you stated what error you are encountering.

    0 comments No comments

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.