Functions/Add-ManagerEmailAddressProperty.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<#
.SYNOPSIS
    This function adds a ManagerEmailAddress property to a user in Exchange Online
.SYNOPSIS
    This function adds a ManagerEmailAddress property to a user in Exchange Online.
    The property is only added if the user's Manager property is already defined.
    This function assumes that the connection to Exchange Online has already been made.
#>

function Add-ManagerEmailAddressProperty {
    [CmdletBinding(PositionalBinding=$true)]
    param (
        # The user in Exchange Online
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        $user
    )

    # Add the property if the Manager property is defined
    if (![String]::IsNullOrWhiteSpace($user.Manager)) {
        $manager = Get-User -Identity $user.Manager

        # Manager exists, add the manager email address
        if ($manager) {
            $user | Add-Member -NotePropertyName "ManagerEmailAddress" -NotePropertyValue $manager.UserPrincipalName -Force
        }
    }

    # Return the user
    return $user
}