Functions/Remove-GSuiteOrganizationalUnit.ps1

<#
.SYNOPSIS
    This function removes one GSuite organizational unit.
#>

function Remove-GSuiteOrganizationalUnit {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([Bool])]
    param (
        # The full path for the organizational unit.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$path
    )

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

    # Remove the '/' at the beginning of the path
    $path = $path.TrimStart("/")

    # Prepare REST call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://www.googleapis.com/admin/directory/v1/customer/my_customer/orgunits/$($path)"
        Method  = "DELETE"
        Headers = @{
            Accept        = "application/json"
            Authorization = "Bearer $($Global:GSuiteAccessTokensHashTable.OrganizationalUnit)"
        }
    }

    # Invoke the REST call
    $response = Invoke-RestMethod @invokeRestMethodParams

    # If the removal is successful, $response will be an empty string
    if ("" -eq $response) {
        return $true
    }
    else {
        return $false
    }
}