Get-ADUser -Filter

Roger Roger 7,181 Reputation points
2022-01-16T10:47:26.253+00:00

Hi All

i have a csv file in the below format. i am mainly look to export samaccountnames and Userprincipalname. is the below syntax correct.

Names
Roger Fed
Alex M
James K

$list = Import-Csv C:\temp\list.csv
ForEach($user in $list){    
$dn = $user.Names
Get-ADUser -Filter { displayName -like $dn } -Properties DisplayName,GivenName,Surname,SamAccountName,EmailAddress,Userprincipalname, |Select Surname,GivenName,DisplayName,SamAccountName,EmailAddress,Userprincipalname,title,Office,employeeNumber,employeeType,description,co,personalTitle |Export-Csv C:\out\export.csv -NoType -Append }
Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-01-16T15:43:12.387+00:00

    You have a comma at the end of the Get-ADUser parameters, right before the first pipe symbol. That needs to be removed.

    Try this (it uses the filter STRING instead of the alternate "{...}" method -- the quoting is different):

    $props =    "Surname", "GivenName", "DisplayName", "SamAccountName", "EmailAddress", "Userprincipalname", "title",
                "Office", "employeeNumber", "employeeType", "description", "co", "personalTitle"
    
    Import-Csv C:\temp\list.csv |
        ForEach-Object {    
            Get-ADUser -Filter " displayName -like '$($_.Names)' " -Properties $props | 
                Select-Object $props
    } | Export-Csv C:\out\export.csv -NoType
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-01-16T11:50:44.167+00:00

    Hi @Roger Roger ,

    for me it looks good so far.
    As you just query the AD there is no risk to execute the script. The worst that could happen are some error messages showing up.

    Just give it a try and see if the result fits your needs.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

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.