I've been given a list of hundreds of email addresses and asked to output the names, office, & department values to a spreadsheet. I decided to try this using a for each block but for some reason the export-csv isn't working, I end up with an empty csv output file. Here's what I'm doing:
The input CSV file is just a header of EmailAddress and then 1 or more email addresses on a new line; right now I'm testing with only a single address in the input file, but I've tested with a small list of 2 good addresses and 1 non-existant address which also didn't work.
$addr = Import-CSV C:\dir\list.csv
foreach ($mb in $addr){Get-recipient -resultsize unlimited | where {$_.emailaddresses -like $mb.EmailAddress} | select name, @{Name=’EmailAddresses’;Expression={[string]::join(";", ($_.EmailAddresses))}}, displayname, department, office | Export-CSV -NoTypeInformation Export-List.csv}