# Office 365 – Adding email addresses based on Email Address Policy

Email Address Policies are bread and butter in Microsoft Exchange world. If you work with On-Premise Exchange 2007, Exchange 2010,…

blog

02 Aug 2016 en

![Office 365 – Adding email addresses based on Email Address Policy](/wp-content/uploads/2016/07/Office-365.png)

**Email Address Policies** are bread and butter in **Microsoft Exchange** world. If you work with On-Premise **Exchange 2007**, **Exchange 2010**, **Exchange 2013/2016** you most likely set it up more than once. Unfortunately if you're **Office 365 Exchange Online** Administrator and don't have **On-Premise Exchange in Hybrid scenario** to use, Microsoft doesn't give you any **GUI** option to do this except editing email addresses one by one.

Doing any modifications via **PowerShell** is also gonna set you back couple of hours as well. Recently we've been asked by customer with 20 accounts to create and manage on **Office 365** with multiple domains and I didn't want to spend hours on manual labor creating it, I've decided to write a little script that does that for me. Hope you will find it useful in your scenarios.

### ✅ Solution

Script takes similar approach known to **Microsoft Exchange** administrators and allows easy configuration of **Email Address Policies** based on templates.

```plain
- "%g.%s@company.org.pl"  # would give przemyslaw.klys@company.org.pl
- "%1g.%s@company.org.pl" # would give p.klys@company.org.pl
- "%1g%s@company.org.pl"  # would give pklys@company.org.pl
- "%3g.%s@company.org.pl" # would give prz.klys@company.org.pl
- "%g.%1s@company.org.pl" # would give przemyslaw.k@company.org.pl
- "%g@company.pl"         # would give przemyslaw@company.pl
```

Adding 3 new domains to Office 365 can be easy with few lines of code:

```plain
cls
$login = Read-Host "Please enter your login (in form of email)"
$password = Read-Host "Please enter your password"
$sessionName = Read-Host "Please enter session name (leave blank for default)"

Connect-Ex -login $login -Password $password -sessionName $sessionName
Add-EmailAddressToUsersFromTemplate -EmailTemplate "%g.%s@company.org.pl" -Commit $false -ExcludeUsers "company@companykatowice.onmicrosoft.com","kalendarz@company.org.pl"
Add-EmailAddressToUsersFromTemplate -EmailTemplate "%1g.%s@company.org.pl" -ExcludeUsers "company@companykatowice.onmicrosoft.com"
Add-EmailAddressToUsersFromTemplate -EmailTemplate "%1g%s@company.org.pl" -ShowSummaryTable $true -ShowSummaryLines $true -Commit $true -ExcludeUsers "company@companykatowice.onmicrosoft.com","kalendarz@company.org.pl","przemyslaw.klys@companykatowice.onmicrosoft.com"
Disconnect-EX -sessionName $sessionName
```

Which should give similar input to one below

[!\[Office 365 PowerShell output showing users receiving new email addresses from the script\](/wp-content/uploads/2016/08/img_57a0f689eb237.png)](/wp-content/uploads/2016/08/img_57a0f689eb237.png)

Full description of the script mimicking functionality of **Email Address Policies** can be found at [Office365-AddBulkEmailAddresses.ps1](https://evotec.xyz/hub/scripts/office365-addbulkemailaddressses-ps1/)

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [Creating Visual Indicators for spoofed / external emails with PowerShell](/blog/creating-visual-indicators-for-spoofed-external-emails-with-powershell/)
- [No account settings were returned from the Autodiscover response – Microsoft Exchange 2016](/blog/no-account-settings-were-returned-from-the-autodiscover-response-microsoft-exchange-2016/)
- [Microsoft Exchange Approval Assistant – Your message couldn’t be delivered because delivery to this address is restricted to authenticated senders](/blog/microsoft-exchange-approval-assistant-your-message-couldnt-be-delivered-because-delivery-to-this-address-is-restricted-to-authenticated-senders/)

<!-- PowerForge markdown artifact sha256:F046E9B74D2293298E3169359D508C57C351824AA90D0ABCEFFBE8AA3450F1DF -->
