Currently I have a script that I am able to interrogate an OU (which I define in the script) which shows Username, Name of the user, AD Group Name and the Description of that AD group into a CSV file:
$ou = 'distinguishedName of my OU'
Get-ADGroup -Filter * -SearchBase $ou -Properties Description | ForEach-Object {
foreach($member in Get-ADGroupMember $_) {
[pscustomobject]@{
SamAccountName = $member.SamAccountName
Name = $member.Name
GroupName = $_.Name
Description = $_.Description
}
}
} | Export-csv C:\Users\Me\Desktop\MyFile.csv -NoTypeInformation
When I try to pull of the email addresses of the users as well in the same script I get an error.
$ou = 'distinguishedName of my OU'
Get-ADGroup -Filter * -SearchBase $ou -Properties 'Description', 'EmailAddress' | ForEach-Object {
foreach($member in Get-ADGroupMember $_)
{
[pscustomobject]@{
SamAccountName = $member.SamAccountName
Name = $member.Name
EmailAddress = $_.EmailAddress
GroupName = $_.Name
Description = $_.Description
}
}
} | Export-csv C:\Users\Me\Desktop\MyFile.csv -NoTypeInformation
The error message states the script fails around this point of the script:
-Properties 'Description', 'EmailAddress'