# Azure AD Connect – Synchronizing MAIL field with UserPrincipalName in Azure

Azure AD Connect is an application responsible for synchronizing Active Directory with Azure AD allowing for a natural population of users, groups, and devices in Office 365. While for most companies standard setup is very easy and most of the time touch-free, there are companies which require greater customization. During installation of AD Connector, you choose what should be used for Azure AD Username from your AD. UserPrincipalName field is an obvious choice for this and also proposed by default for that purpose. This field is utilized further by your users to log in to your Exchange, SharePoint, Teams and so on.

blog

09 Nov 2018 fr

![Azure AD Connect – Synchronizing MAIL field with UserPrincipalName in Azure](/wp-content/uploads/2018/11/img_5be5ee7e2f3e1.png)

[Voir les images (12)](#pf-content-image-1-0)

**Azure AD Connect** is an application responsible for synchronizing Active Directory with Azure AD allowing for a natural population of users, groups, and devices in Office 365. While for most companies standard setup is very easy and most of the time touch-free, there are companies which require greater customization. During installation of AD Connector, you choose what should be used for **Azure AD Username** from your AD. **UserPrincipalName** field is an obvious choice for this and also proposed by default for that purpose. This field is utilized further by your users to log in to your **Exchange**, **SharePoint**, **Teams** and so on.

[!\[Azure AD Connect sign-in configuration showing UserPrincipalName selected as the Azure AD username\](/wp-content/uploads/2018/11/img_5be5b6e874a9f.png)](/wp-content/uploads/2018/11/img_5be5b6e874a9f.png)

As seen on the screen above you can pick that field during installation of AD Connector. If at that point you decide you want to change that field you can do so. But what if you decide to change this setting after you've already had Azure AD Connect up and ready? It's not that obvious but there is a way!This is where Synchronization Rules Editor comes in handy. Using this tool you can change that. You can change a lot of mappings if you wish to do so. Here's how?

### 💡 Synchronization Rules Configuration

Basically when you open **Synchronization Rules Editor** there are multiple rules to choose from

[!\[Synchronization Rules Editor listing inbound and outbound synchronization rules in Azure AD Connect\](/wp-content/uploads/2018/11/img_5be5e666aa8d3.png)](/wp-content/uploads/2018/11/img_5be5e666aa8d3.png)

We're mostly interested in the **connector** that is defined for our domain, is related to user object type and finally has **userPrincipalName **somewhere in it.

[!\[Filtered synchronization rules showing userPrincipalName mappings for the selected domain connector\](/wp-content/uploads/2018/11/img_5be5e71d6cd49.png)](/wp-content/uploads/2018/11/img_5be5e71d6cd49.png)

That leaves as only 2 with **Synchronization Rules.** When you now edit those two rules, you will be given an option to write directly or clone that rule and use it separately. It's up to you to decide on the best path, but it's highly advised to use the clone and merely disable the rule as suggested. It's a matter of answering **Yes** to the next question.

[!\[Azure AD Connect prompt asking whether to edit the built-in rule directly or create a cloned rule\](/wp-content/uploads/2018/11/img_5be5e82e6cf61.png)](/wp-content/uploads/2018/11/img_5be5e82e6cf61.png)

If you clone this rule, you have to edit **Name**, **Description** and mandatory **Precedence **fields in the next Window. The lower **precedence** the higher in the hierarchy. That means in our case we should use something below 100 to make sure it's first in line.

[!\[Edit inbound synchronization rule window with custom name, description, and precedence below 100\](/wp-content/uploads/2018/11/img_5be5e962aac09.png)](/wp-content/uploads/2018/11/img_5be5e962aac09.png)

After updating Description block we now can skip to Transformations. This is where the magic happens. We're basically interested in **Expression** over Target Attribute **UserPrincipalName**

[!\[Transformations tab showing the UserPrincipalName target attribute expression in a cloned synchronization rule\](/wp-content/uploads/2018/11/img_5be5ea08cf097.png)](/wp-content/uploads/2018/11/img_5be5ea08cf097.png)

We need to modify source expression to match our needs. Whether this will be **mail **to **UserPrincipalName** field or vice versa, the change is pretty simple. You merely swap one field with another like shown in the code box below

```powershell
IIF(IsPresent([userPrincipalName]),[userPrincipalName], IIF(IsPresent([sAMAccountName]),([sAMAccountName]&"@"&%Domain.FQDN%),Error("AccountName is not present")))
```

To

```powershell
IIF(IsPresent([mail]),[mail], IIF(IsPresent([sAMAccountName]),([sAMAccountName]&"@"&%Domain.FQDN%),Error("AccountName is not present")))
```

While at first look it seems complicated it's quite simple equation. If the **mail **field is present use mail field. If it's not present check if **SamAccountName** is present. If it is present use **SamAccountName** with domain field. If **SamAccountName **is not present either throw an error. After replacing one equation with the other, we have to save this rule and go into the next one. As you can see on the screen below, there are three rules now, but only 2 of those are still enabled. Cloned one and the other one that we need to modify.

[!\[Synchronization Rules Editor showing the first cloned rule enabled alongside the remaining original rule\](/wp-content/uploads/2018/11/img_5be5ebda0607b.png)](/wp-content/uploads/2018/11/img_5be5ebda0607b.png)

Finally, after editing those two rules we should be left only with two cloned, but enabled rules, and two disabled rules.

[!\[Final synchronization rules list with both cloned rules enabled and the original rules disabled\](/wp-content/uploads/2018/11/img_5be5ee7e2f3e1.png)](/wp-content/uploads/2018/11/img_5be5ee7e2f3e1.png)

What's now left is a simple **PowerShell** command to make **full**, **initial** synchronization.

```powershell
Start-ADSyncSyncCycle –PolicyType Initial
```

Give it a few minutes and verify your changes.

### 💡 Small explanation on behaviour

If **Email** Address field is empty connector will use **SamAccountName**. Similarly, if you use **UserPrincipalName** and it will be unable to use **UserPrincipalName** due to not existing domain name it will use that field. In the example below user has an empty E-Mail field but below example applies to any Azure UserName that you chose for synchronization.

[!\[Azure portal user profile showing an empty Mail field before the Azure AD Connect change\](/wp-content/uploads/2018/11/img_5be5d5bc77650.png)](/wp-content/uploads/2018/11/img_5be5d5bc77650.png)

Will get you this

[!\[Azure portal sign-in name falling back to samAccountName when the Mail field is empty\](/wp-content/uploads/2018/11/img_5be5d5eae84ef.png)](/wp-content/uploads/2018/11/img_5be5d5eae84ef.png)

But if you fill it in properly with an email address and execute **Delta** synchronization on your **AD Connect**

[!\[Active Directory user properties with the email address filled in before running a delta synchronization\](/wp-content/uploads/2018/11/img_5be5d6384249a.png)](/wp-content/uploads/2018/11/img_5be5d6384249a.png)

```powershell
Start-ADSyncSyncCycle –PolicyType Delta
```

It will update itself to set values adequately.

[!\[Azure user account updated after delta synchronization so the sign-in name matches the Mail field\](/wp-content/uploads/2018/11/img_5be5d70569f99.png)](/wp-content/uploads/2018/11/img_5be5d70569f99.png)

## Projets associés

Projets liés aux tags, catégories et au contenu de cet article.

[Tous les projets](/fr/projets/)

Recherche des projets associés...

[Retour au blog](/blog)

## Articles associés

- [Azure ADConnect Export Failed – Permission-issue error](/fr/blog/azure-adconnect-export-failed-permission-issue-error/)
- [Report Active Directory Accounts that are Synchronized with Azure AD](/fr/blog/report-active-directory-accounts-that-are-synchronized-with-azure-ad/)
- [Free Microsoft Azure/Office365 Fundamentals Training starting February 6th 2023](/fr/blog/microsoft-fundamental-free-training-starting-february-6th-2023/)

<!-- PowerForge markdown artifact sha256:78A2AF9B4BF25A40F888DBC10368802D338C980E81DFED1A6F9F9AF16C61EB56 -->
