Manage Email address using PowerShell | Office 365 5/5 (3)

The current article dedicated to reviewing the PowerShell command syntax that we use for – manage E-mail address in Exchange Online-based environment.
We will learn how to create a new E-mail, remove existing E-mail address, add alias E-mail address, and add a primary E-mail address.

A couple of notes on the subject of email address in Office 365 environment:

  1. UPN Address = Primary email address
    In Office 365 environment, the User Login Name (UPN) becomes by default the user primary email address.
  2. User UPN updates.
    In case that we change the Office 365 user names (the user alias or the user domain suffix), the change will be replicated automatically to the Exchange Online infrastructure and the user primary email address will change also.
  3. Change the user primary email address
    There is no way of changing user primary email address by using the Exchange Online web interface. The only way is to use PowerShell command.
  4. onmicrosoft domain email address
    By default, each of the Office 365 users will have the “onmicrosoft” email address. In case that the user has “Public email address,” the “onmicrosoft” email address will become an alias email address.
  5. Email address type
    An Office 365 users can have many types of email address such as SMTP, X.500, X.400, etc.
  6. Number of email addresses
    An Office 365 users can have many email addresses from the same type. For example, Office 365 user can have the email address John@o365info.com and additional email address: Jobs@o365info.com
  7. Primary versus secondary email address
    Only one email address could set as a Primary E-mail address. All the reset email address describes as Alias, proxy or secondary email address. To be able to distinguish the Primary SMTP email address from the “additional” (secondary) email address, the Primary SMTP email address will be saved with the “SMTP” (in capital letter) prefix.
  8. Email address as a “unique” identifier
    There is no option to allocate the same email address to more than one user\group.

PowerShell | Help & additional information

In case that you are a novice in the PowerShell environment, you can use the following link to get more information about the “first steps” such as: downloading the required PowerShell
software components, how to use the PowerShell console, running a PowerShell script, etc.

Read more
Link Table

PowerShell Naming Conventions & general information

If you want to get more information about the Naming Conventions that we use for this article and get some general tips about: how to work with the PowerShell, read the article: Help and additional information – o365info.com PowerShell articles

Create remote PowerShell session

Before we can use the required PowerShell commands, we need to download and install the Office 365 cmdlets + create remote PowerShell session to Office 365 or Exchange Online. If you need more information about how to create a remote PowerShell session read the following articles: Part 2: Connect to Office 365 by using Remote PowerShell and Part 3: Connect to Exchange Online by using Remote PowerShell

How to use a PowerShell script

Most of the PowerShell articles include a PowerShell script that simplifies the use of the PowerShell commands. If you want to get more information about: How to use a PowerShell script, read the article: Connect to Office 365 and Exchange Online using a script

PowerShell command and Script languish in more details

If you are new to the PowerShell world, you can read more information about PowerShell in Office 365 environment in the article: The Power of PowerShell


1. Add/Set Email Address

1.1 – Set Primary email address

PowerShell command Syntax

PowerShell command Example

1.2 – Set Primary email address and Add Alias address

PowerShell command Syntax

PowerShell command Example

1.3 – Set Primary email address + Alias address (“Mark” the Primary email address )

Another option that we can use for – set email address as a primary email address is to
add the “SMTP” string before the email address.
In the following example: the email address: john@o365info.com will be set as a primary email address.

PowerShell command Syntax

PowerShell command Example

1.4 – Set Alias Email address (Method 1)

PowerShell command Syntax

PowerShell command Example

Set Alias Email address (Method 2)

PowerShell command Syntax

PowerShell command Example

1.5 – Add Alias Email address (Bulk Mode)

In some scenario, we need to add an email address to all the users (Bulk mode). For example: organization that has a couple of public domain names.
The administrator wants to add each of the public domain names to each of the Office 365 recipients. In the following example, we add the “additional email address.”
To Office 365 recipients. The “additional E-mail address” will be based on the alias user name, and the “suffix” of the email address will be “o365info.com”.

Note – before we can excite the PowerShell command,
we will need to verify that the domain name was registered (added) to the Office 365 account.

PowerShell command Syntax

1.6 – Add Alias Email address – by using CSV File (Bulk Mode)

Additional implementation of bulk mode is by using a data from a CSV file. The CSV file includes predefined “Row headers” and values.
In our example, we create a CSV file that includes the following row headers: UserID, NewAddress, and Proxy1.

The PowerShell command will access the CSV file and “pull out” the required values.

PowerShell command Syntax

In the following screenshot, we can see the structure of the CSV file.
The “Row header” that we use could be named in any way that you preferred
(Avoid using row header name that contains spaces).

We use the option of the CSV file for adding additional email address (Proxy address) to the Office 365 recipients that appear in the CSV file.
Add Alias Email address – by using CSV File (Bulk Mode )

you can download the a sample of the CSV file by using the following link: Download CSV File

1.7 – Add X.500 Email address

Most of the time, when we mention the term “Email address” we referee to SMTP Email address. In some scenarios
such as Migration from Exchange environment or another mail system that
stores the information about users in the LDAP directory, we will need to add (or remove) X. 500 addresses to the Office 365 recipients.

PowerShell command Syntax

PowerShell command Example


2. Remove Email Address

2.1 – Remove Email address (Method 1)

PowerShell command Syntax

PowerShell command Example

2.2 – Remove Email address ( Method 2)

PowerShell command Syntax

PowerShell command Example

2.3 – Remove X.500 Email address

PowerShell command Syntax

PowerShell command Example


3. Display/Export information about Email Address

The user property described as: Email address, can contain more than one email address
(Primary email address and proxy email address) and many types of email address types such as: SMTP, X.500, etc.,
In other words, we can say that the Email address property, can be considered as an “array” that contains multiple values (Email addresses).

When we use a PowerShell command for displaying information about particular user property such as – E-mail Address, the information that displayed on the screen cannot contain by default all the information that contained within the E-mail address property.

In the following screenshot we can see the output of the PowerShell command:
Get-Mailbox | Select UserPrincipalName,DisplayName,Name,EmailAddresses
We get only a partial information about the email addresses.

Display-Export information about Email Address standard view

3.1 – Display Email address information on screen – Using the Wrap parameter

To be able to display all of the information in the PowerShell command syntax:

PowerShell command Syntax

Display Email address information on screen - Using the Wrap parameter

3.2 – Display Email address information on screen – Using the Expand parameter

PowerShell command Syntax

PowerShell command Example

Export information about email address to file

In the following section, we review the option that we can use for exporting information about an Office 365 users and their mail addresses.
Exporting information to a CSV enables us to edit or “manipulate” the information by using applications
such as – Excel. The option of exporting information to HTML file is more suitable for reports.

3.3 – Export information about email address to CSV file

PowerShell command Syntax

PowerShell command Example

3.4 – Export information about email address to HTML file

When using the default option of exporting data to HTML file, the output is a little row.
To make the output of the HTML more “readable,” we can use a “prefix” that the standard export PowerShell command that will handle the design of the HTML output. Because the command contains many lines and characters, the best practice is to use this syntax by using a PowerShell script.
At the bottom of the post, you can find a PowerShell script that includes the option of exporting the information to HTML.

In the following screenshot, we can see the content of the HTML file when using the “design” instructions”:

Export information to HTML

PowerShell command Syntax

Exchange Online users Mailboxes Details

” | Out-File C:\TEMP\Get-Mailbox.html

4. Download the Manage Email address PowerShell menu script

For your convenience, I have “Wrapped” all the PowerShell commands that were reviewed in a PowerShell Script named: Manage email address.ps1
You are welcome to download the script and use it.

download-button-02.png

PowerShell command syntax – Office 365 | Article series index

Now it’s Your Turn!
It is important for us to know your opinion on this article


Summary
Article Name
Manage Email address using PowerShell | Office 365
Description
The current article dedicated to reviewing the PowerShell command syntax that we use for – manage E-mail address in Exchange Online-based environment. We will learn how to create a new E-mail, remove existing E-mail address, add alias E-mail address, and add a primary E-mail address.
Author
Publisher Name
o365info.com
Publisher Logo

Please rate this

Print Friendly

Related Post

Eyal Doron on EmailEyal Doron on FacebookEyal Doron on GoogleEyal Doron on LinkedinEyal Doron on PinterestEyal Doron on RssEyal Doron on TwitterEyal Doron on WordpressEyal Doron on Youtube
Eyal Doron
Share your knowledge.
It’s a way to achieve immortality.
Dalai Lama

20 Responses to “Manage Email address using PowerShell | Office 365”

  1. Hi
    the post is not updated.
    primary smtp can be set now through the web console .

  2. Garry Schindler Reply

    Hi,
    I cannot run any of the Set-Mailbox command without getting the error “The term ‘set-mailbox’ is not recognized as the name of cmdlet. I get the same results on 3 different units. The get-msoluser works fine. Do you know how I resolve this?

    • Hello
      You need to create a separate remote PowerShell session to Exchange Online
      Option 1: read the following article to get information about the way to create a remote PowerShell session to Exchange Online
      Using Remote PowerShell to manage Office 365 | Part 2/3

      Option 2: at the bottom of the article you can find a script that will automatically create a remote PowerShell to Windows Azure Active Directory (Office 365) + Exchange Online
      Eyal Doron

  3. Joseph Wendorf Reply

    I’m getting an error message when trying to launch the ‘Manage Email Address.ps1’ script. It doesn’t recognize the command ‘$mailbox’ Line:570 Char: 86

    At C:\Manage Email Address.ps1:570 char:86
    + … + “@$DomName” $mailbox.EmailAddresses += $newaddress
    + ~~~~~~~~
    Unexpected token ‘$mailbox’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

    Any help on this?

    The other script are working perfectly, this the only one I’m having issues with.

    Thanks for help!

  4. I would like to know the powershell script to remove all the secondary(proxy) email addresses for all users.

    Regards,
    Ankur

  5. I am having same difficult while running the script.

    It doesn’t recognize the command ‘$mailbox’ Line:570 Char: 86

    At C:\Manage Email Address.ps1:570 char:86
    + … + “@$DomName” $mailbox.EmailAddresses += $newaddress
    + ~~~~~~~~
    Unexpected token ‘$mailbox’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

    Please help us regarding that. I would highly appreciate that.
    thanks.

  6. About the error on line 570:

    The line has to be slit op in 2 like this:
    $a = foreach ($mailbox in $mailboxes) { $newaddress = $mailbox.alias + “@$DomName”
    $mailbox.EmailAddresses += $newaddress

  7. I am really delighted to glance at this website
    posts which consists of lots of useful data, thanks for providing these information.

  8. essay writing services Reply

    What’s up to all, the contents present at this site are
    actually awesome for people knowledge, well, keep up the nice
    work fellows.

  9. You’ve made some decent points there. I looked on the internet for more info about
    the issue and found most people will ggo along with our views
    on this web site.

Leave a Reply

Your email address will not be published. Required fields are marked *