Disconnect-ExchangeSessions.ps1
19 Sep 2025Description
Purpose
Disconnects all active Exchange sessions and connections.
Detailed Description
The Disconnect-ExchangeSessions function disconnects all active Exchange sessions and connections. It first retrieves all active PowerShell sessions using the Get-PSSession cmdlet, and then retrieves all active Exchange connections using the Get-ConnectionInformation function. It then iterates through each session and connection, and disconnects them if they are in the ‘Opened’ or ‘Connected’ state respectively.
Usage
Example 1
Disconnect-ExchangeSessions
Disconnects all active Exchange sessions and connections.
Notes
Author: Your Name Date: Today’s Date
Script
<#
.SYNOPSIS
Disconnects all active Exchange sessions and connections.
.DESCRIPTION
The Disconnect-ExchangeSessions function disconnects all active Exchange sessions and connections. It first retrieves all active PowerShell sessions using the Get-PSSession cmdlet, and then retrieves all active Exchange connections using the Get-ConnectionInformation function. It then iterates through each session and connection, and disconnects them if they are in the 'Opened' or 'Connected' state respectively.
.PARAMETER None
This function does not accept any parameters.
.EXAMPLE
Disconnect-ExchangeSessions
Disconnects all active Exchange sessions and connections.
.NOTES
Author: Your Name
Date: Today's Date
#>
function Disconnect-ExchangeSessions {
$sessions = Get-PSSession
$connections = Get-ConnectionInformation
foreach ($session in $sessions) {
if ($session.State -eq 'Opened') {
Write-Output "Disconnecting session: $($session.Name)"
Remove-PSSession -Session $session
}
}
foreach ($connection in $connections) {
if ($connection.State -eq 'Connected' -and $null -ne $connection.ConnectionId) {
Write-Output "Disconnecting connection: $($connection.ConnectionId)"
Disconnect-ExchangeOnline -ConnectionId $connection.ConnectionId -Confirm:$false
}
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.