# Module: Orbit
# Function: Session
# Author: David Eberhardt
# Updated: 27-MAY 2023
# Status: RC

function Disconnect-Orbit {
    Disconnects all sessions for Graph & MicrosoftTeams
    Helper function to disconnect from Graph & MicrosoftTeams
    By default Office 365 allows two (!) concurrent sessions per User.
    Session exhaustion may occur if sessions hang or incorrectly closed.
    Avoid this by cleanly disconnecting the sessions with this function before timeout
  .PARAMETER DisableAdminRoles
    Disables activated Admin roles before disconnecting from Azure Ad

    Disconnects from Graph, MicrosoftTeams
    Errors and Warnings are suppressed as no verification of existing sessions is undertaken
    Helper function to disconnect from AzureAD & MicrosoftTeams
    To disconnect from ExchangeOnline, please run Disconnect-ExchangeOnline
    By default Office 365 allows two (!) concurrent sessions per User.
    If sessions hang or are incorrectly closed (not properly disconnected),
    this can lead to session exhaustion which results in not being able to connect again.
    An admin can sign-out this user from all Sessions through the Office 365 Admin Center
    This process may take up to 15 mins and is best avoided, through proper disconnect after use
    An Alias is available for this function: dis
    Disconnects existing connections to Graph and MicrosoftTeams

    [Parameter(HelpMessage = 'Disables active AzureAd Admin Roles')]
  ) #param

  begin {
    Show-OrbitFunctionStatus -Level RC
    Write-Verbose -Message "[BEGIN ] $($MyInvocation.MyCommand)"

    $scriptErrorActionPreference = $ErrorActionPreference
    $ErrorActionPreference = 'SilentlyContinue'

    $scriptWarningPreference = $WarningPreference
    $WarningPreference = 'SilentlyContinue'

    $scriptInformationPreference = $InformationPreference
    $InformationPreference = 'Continue'

    # Cleanup of global Variables set done at the end
    [bool]$sessionFound = $false

  } #begin

  process {
    Write-Verbose -Message "[PROCESS] $($MyInvocation.MyCommand)"

    # Querying Azure Ad Connection
    $SessionInfo = Get-CurrentConnectionInfo
    if ( $SessionInfo.Tenant ) {
      Write-Information "Disconnecting from Tenant: $($SessionInfo.Tenant)"

    try {
      Write-Verbose -Message 'Disconnecting Session from MicrosoftTeams'
      $null = (Disconnect-MicrosoftTeams)

      if ($DisableAdminRoles) {
        Write-Verbose -Message 'Disabling activated Admin Roles'
        $null = (Disable-MyAzureAdAdminRole)

      Write-Verbose -Message 'Disconnecting Session from Graph'
      $null = (Disconnect-MgGraph)
    catch {
      throw $_

    Write-Verbose -Message 'Cleaning up PowerShell Sessions'
    $PSSessions = Get-PSSession -WarningAction SilentlyContinue

    foreach ($session in $PSSessions) {
      if ($session.ComputerName -like '*' -or $session.ComputerName -eq '') {
        $sessionFound = $true
        Remove-PSSession $session

    if ( $sessionFound ) {
      Get-Module | Where-Object { $_.Description -like '**' } | Remove-Module
    else {
      Write-Verbose -Message 'No remote PowerShell sessions currently exist'

    Set-PowerShellWindowTitle 'Windows PowerShell'

  } #process

  end {
    # Reset the Preference Variables
    $ErrorActionPreference = $scriptErrorActionPreference
    $WarningPreference = $scriptWarningPreference
    $InformationPreference = $scriptInformationPreference

    # Cleanup of global Variables set
    $null = (Remove-Variable -Name OrbitQuery* -Scope Global -ErrorAction SilentlyContinue) # Transient Tenant Information to be removed

    Write-Verbose -Message "[END ] $($MyInvocation.MyCommand)"
  } #end

} #Disconnect-Orbit