Functions/Add-AzureADUserManagerEmailAddressProperty.ps1

<#
.SYNOPSIS
    This function adds a ManagerEmailAddress property to a user in Azure AD.
.SYNOPSIS
    This function adds a ManagerEmailAddress property to a user in Azure AD.
    The property is only added if the user's Manager property is already defined.
    This function assumes that the connection to Azure AD has already been made.
#>

function Add-AzureADUserManagerEmailAddressProperty {
    [CmdletBinding(PositionalBinding=$true)]
    param (
        # The user in Azure AD.
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNull()]
        [PSCustomObject]$user
    )

    # Add the property if the user has a manager
    try {
        $manager = Get-AzureADUserManager -ObjectId $user.ObjectId -ErrorAction Stop
        if ($manager) {
            $user | Add-Member -NotePropertyName "ManagerEmailAddress" -NotePropertyValue $manager.UserPrincipalName -Force
        }
    }
    catch {
        # Do nothing
    }

    # Return the user
    return $user
}