Get-ADUser -Filter

Roger Roger 5,446 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 Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,607 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,432 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,230 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,459 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,831 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