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
    if (!$Global:GSuiteAccessTokensHashTable) {
        throw "You must call the Connect-GSuiteAdminAccount cmdlet before calling any other GSuite cmdlets."
    }

    # Validate that the user access token exists in the hash table
    if ([String]::IsNullOrWhiteSpace($Global:GSuiteAccessTokensHashTable.User)) {
        throw "User access token is required to call Add-GSuiteUserAlias."
    }

    # 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
}