I have an interesting problem that I am trying to solve. We have recently sold a portion of our company to another company and I need to export the AD and mail information about their accounts into a spreadsheet. I am going to use this information to create a set of contacts that forward any email sent to their old address to their new address. Below is what I have been able to do so far. I get a collection of objects with the get-recipient and pipe it to a forEach. Inside of the ForEach I get the fields that I need. I have to get the email addresses separately because they will not resolve in the export-csv in less I convert them to text. If I have the export-csv line inside of the ForEach it will overwrite the previous record. If I have it on the outside I don't get anything because of the scope of the $mbdata variable is local to the ForEach. I don't want to use out-file because it would mess up the formatting. Does anyone have any thoughts on how I should go with this script?
get-recipient -filter "displayname -like '*(Company name)'" -resultsize 3000 | `
ForEach-Object {
$mbdata = $_| select Name,alias,company,customattribute5,externalemailaddress,displayname, `
firstname,organizationalunit,phone,primarysmtpaddress,recipienttype,distinguishedname, `
identity,database,emailaddresses
$mbdata.emailaddresses = [string]::join("`t", ($mbData.EmailAddresses) )
Export-Csv .\test.csv -inputobject $mbdata -noTypeInformation
}
'done!'