Functions/New-GSuiteGroup.ps1

<#
.SYNOPSIS
    This function creates a GSuite group.
#>

function New-GSuiteGroup {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([PSCustomObject])]
    param (
        # The name for the group.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$name,

        # The primary email address for the group.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$primaryEmailAddress
    )

    # 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 group access token exists in the hash table
    if ([String]::IsNullOrWhiteSpace($Global:GSuiteAccessTokensHashTable.Group)) {
        throw "Group access token is required to call New-GSuiteGroup."
    }

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

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