Private/Auth/Disconnect-GraphSession.ps1

# Copyright (c) 2026 Sandy Zeng. All rights reserved.
# Source-available. All rights reserved. See LICENSE file.

<#
    Disconnect-GraphSession.ps1 — Signs out from Microsoft Graph and clears session state.
 
    Author: Sandy Zeng
    Project: IntuneDiff
 
    Version History:
    1.0.0 Initial release.
    1.0.2 Full session cleanup on window close; module state cleared on sign-out.
    2.0.0 No longer depends on Microsoft.Graph.Authentication.
#>


function Disconnect-GraphSession {
    <#
    .SYNOPSIS
        Signs out the current account, removes it from the in-memory cache,
        and clears module state. Use -KeepCache to keep the account available
        for silent re-sign-in.
    #>

    [CmdletBinding()]
    param([switch]$KeepCache)

    try {
        if ($script:GraphCurrent -and $script:GraphAccounts.ContainsKey($script:GraphCurrent)) {
            $homeId = $script:GraphCurrent
            if (-not $KeepCache) {
                $script:GraphAccounts.Remove($homeId)
            }
        }
    } finally {
        $script:GraphCurrent = $null
        $script:SignedInUser = $null
    }
}