Functions/Add-GSuiteUserAlias.ps1

<#
.SYNOPSIS
    This function adds an alias for a GSuite user.
#>

function Add-GSuiteUserAlias {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([PSCustomObject])]
    param (
        # The primary email address of the user.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$primaryEmailAddress,

        # The new alias for the user.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$alias
    )

    # Validate that the 'connection' has been established and the user access token exists
    Assert-GSuiteConnection -Scope "User"

    # Prepare REST call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://www.googleapis.com/admin/directory/v1/users/$($primaryEmailAddress)/aliases"
        Method  = "POST"
        Headers = @{
            "Content-Type" = "application/json"
            Accept         = "application/json"
            Authorization  = "Bearer $($Global:GSuiteAccessTokensHashTable.User)"
        }
        Body    = @{
            alias = $alias
        } | ConvertTo-Json
    }

    # Invoke the REST call
    return Invoke-RestMethod @invokeRestMethodParams
}