Apologies for the basic question, I like to dabble in Powershell but as I don't use it frequently enough I have forgotten a lot!
I have a csv list of users' employee numbers (employee.csv), which are in AD under "extensionAttribute15"
I would like, for each employee number in the list, to return each accounts SamAccountName and what their AD group membership is, into an Excel sheet
So far I have
$employee = Import-csv .\Downloads\employee.csv
$employee | ForEach-Object {
Get-ADUser -Credential $Credentials -Filter "extensionAttribute15 -like '*$($_.extensionAttribute15)*'" -Properties * | Select-Object extensionAttribute15, Name, emailAddress, samAccountName, Description, MemberOf | Export-Csv .\Downloads\new_employee.csv
}
which works, but it doesn't expand on the AD Group membership...
Thanks in advance!
As commented,
Get-ADUser
already returns objects with these properties:DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
.In your case you also want these properties to be returned:
extensionAttribute15, EmailAddress, Description and MemberOf
A flaw in your code is that you
Export-Csv
inside the loop, so on each iteration it will be overwritten. You can avoid that by either add switch-Append
there, but then you are creating a lot of disk-write actions.The best way is to pipe everything to Export-Csv at the end of the
ForEach-Object
loop, so the file needs to be written only once.You can combine the values of the MemberOf property into one column if you like by changing the
Select-Object
line into