Scripting question - get email address associated with a list of AD Groups.

I have a list of AD Groups that have an email address associated with them in the LDAP "Mail" attribute.  These groups are used to enable membership for Exchange distrubution groups.

We are in the process of auditing the group names and email addresses and need to pull a list of email addresses for a known list of AD groups.  The group list is in .txt format and consists of one group name per line separated with a carriage return.

I'm attempting to use the following script:

Get-Content 'C:\Temp\inputfilename.txt' | ForEach-Object{ Get-ADGroup -Identity $_ -properties mail | select name,mail | sort mail | Export-Csv -Path 'C:\temp\outputfilename.csv' -NoTypeInformation}

Powershell seems to be listing the members for the various groups in an error state in the console and not returning anything to the output file.


Get-ADGroup : Cannot find an object with identity: '"usernameofusersanitizedfortheinternet","NameofUser (usernameofuser)","NameofADGroup"' under: 'DC=XX,DC=XX,DC=XX'.
At line:1 char:56
+ ... ' | ForEach-Object{ Get-ADGroup -Identity $_ -properties mail | selec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ("usernameofuser","Nameofuser..."ADGroupName":ADGroup) [Get-ADGroup], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetA

Any thoughts?  I can get this to work as a simple script without the loop.  Thanks in advance for your help!