Functions/Disconnect-SharePointOnline.ps1

<#
.SYNOPSIS
    This function disconnects from SharePoint Online.
#>

function Disconnect-SharePointOnline {
    [CmdletBinding()]
    [OutputType([Bool])]
    param ()

    # Check for existing connection
    if (!$Global:SharePointOnlineConnectionEstablished) {
        Write-Warning "There is no open connection to SharePoint Online."
        return $true
    }

    # Close the connection
    try {
        Disconnect-SPOService
        $Global:SharePointOnlineConnectionEstablished = $false
        Write-Information "Disconnected from SharePoint Online."
        return $true
    }
    catch {
        Write-Error "Exception occurred while disconnecting from SharePoint Online.`r`n$($_.Exception.Message)"
        return $false
    }
}