Forum Discussion

Kieran_Mees's avatar
Kieran_Mees
Copper Contributor
Apr 04, 2022

Take CSV with UPNs and add DisplayName, PrimarySMTPAddress and GUID

 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.

  • 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
  • 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.
    • Kieran_Mees's avatar
      Kieran_Mees
      Copper Contributor

      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.

      • Kieran_Mees's avatar
        Kieran_Mees
        Copper Contributor
        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

Resources