Functions/New-GSuiteOrganizationalUnit.ps1

<#
.SYNOPSIS
    This function creates a GSuite organizational unit.
    If the parent path is '/parent' and the name is 'name', the full path of the new OU will be '/parent/name'.
#>

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

        # The parent path of the organizational unit.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$parentPath
    )

    # Validate that the 'connection' has been established and the organizational unit access token exists
    Assert-GSuiteConnection -Scope "OrganizationalUnit"

    # If the parent path doe not start with '/', insert '/' to the front of it
    if (!$parentPath.StartsWith('/')) {
        $parentPath = $parentPath.Insert(0, "/")
    }

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

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