SOLVED

Take CSV with UPNs and add DisplayName, PrimarySMTPAddress and GUID

Copper Contributor

 Hi,

I'm currently looking for a way to amend my code

 

Get-Mailbox -Resultsize Unlimited | select DisplayName,PrimarySMTPAddress,UserPrincipalName,GUID| Export-csv C:\O365Users.csv

 

So that this takes from a CSV with a selection of UPNs and outputs the DisplayName, PrimarySMTP and GUID of only those from that imported CSV.


Is this possible and how would I go about it?
I'm very new to PowerShell so any and all help would be great.

3 Replies
Try something like this:

Import-CSV blabla.csv | % { Get-Mailbox $_.UPN | select DisplayName,PrimarySMTPAddress,UserPrincipalName,GUID }

where you have a CSV file named blabla.csv, with column UPN to designate the user.

@VasilMichev Ok that seemed to work, however it grabs those values of everyone on the Exchange. I only want the values for the specific ones from the CSV.

 

E.g. the server has 20 users, in my CSV I have 10 users I want the GUID, Primary SMTP and display name for.

Additionally, how would I put this all back into the CSV? When I try to export-csv, I get an error because of the $_ variable and when I try putting "$_ Out-File C:\example.csv" the example.csv only has a weird greek symbol which I assume is an error.

best response confirmed by Kieran_Mees (Copper Contributor)
Solution
I figured it out.
The script I used is as following:

Import-CSV C:\TestCSV.csv|
ForEach-Object {Get-Mailbox -id $_.UserPrincipalName} |
Select Name,DisplayName,UserPrincipalName,GUID,PrimarySMTPAddress |
export-csv -NoTypeInformation c:\TestCSV2.csv
1 best response

Accepted Solutions
best response confirmed by Kieran_Mees (Copper Contributor)
Solution
I figured it out.
The script I used is as following:

Import-CSV C:\TestCSV.csv|
ForEach-Object {Get-Mailbox -id $_.UserPrincipalName} |
Select Name,DisplayName,UserPrincipalName,GUID,PrimarySMTPAddress |
export-csv -NoTypeInformation c:\TestCSV2.csv

View solution in original post