How to get info in csv combine with result from PowerShell cmd?

Hi, I need help to code this in PowerShell:

$Users = Import-Csv "C:\temp\ExchangeMailboxes.csv"

ExchangeMailboxes.csv contains these info: SamAccountName,Mail

How can I get the mail info in ExchangeMailboxes.csv and the result of this cmd:

$Users | where {$_.domainFQDN -eq "mydomain"} | % {Get-ADUser -Identity $_.SamAccountName -Properties admindescription} | Select Name,SamAccountName,AdminDescription,Enabled

How can add mail info in the below cmd. So at the end I should get: Mail,Name,SamAccountName,AdminDescription,Enabled

Thanks
LVL 1
SAM2009Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
In general, you select one of the sources to merge and put it into a hash table, with the joining column as key.
Then you retrieve the other source and merge from the hashtable using a "calculated property".
It's not quite clear, though, where the second "$Users" is coming from (the one you use to retrieve the AD user from; it has a column "domainFQDN" that's not in your description of ExchangeMailboxes.csv.
So something like this (note: this will not work as it is, because $Users is not set here, due to the missing information I indicated above):
$lookup = @{}
Import-Csv "C:\temp\ExchangeMailboxes.csv" | ForEach-Object {$lookup[$_.SamAccountName] = $_}
$Users |
	Where-Object {$_.domainFQDN -eq "mydomain"} |
	ForEach-Object {
		Get-ADUser -Identity $_.SamAccountName -Properties admindescription |
			Select-Object @{n='Mail'; e={$lookup[$_.SamAccountName].Mail}}, Name, SamAccountName, AdminDescription, Enabled
	} | Export-Csv -NoTypeInformation -Path C:\Temp\merged.csv

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SAM2009Author Commented:
Could you please explain me how I should read this:

ForEach-Object {$lookup[$_.SamAccountName] = $_
0
oBdACommented:
$lookup is a hash table. The ForEach-Object takes each row of ExchangeMailboxes.csv, and adds it to the hash table, using the SamAccountName as key.
It's the fastest way to join the tables based on the common property SamAccountName.
0
SAM2009Author Commented:
Thanks a lot. That works fine!

Just a little correction. This need to be added: $Users = Import-Csv "C:\temp\ExchangeMailboxes.csv"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.