To connect to Azure AD using the PowerShell MS Online module, you use the following command:
Connect-MsolService
So far, so good…
But what if you want to disconnect again?
The Exchange Online PowerShell module has:
Disconnect-ExchangeOnline
The Azure AD PowerShell module has:
Disconnect-AzureAD
But this command is missing from the MS Online PowerShell module.
To disconnect from MS Online, you can use the .Net ClearUserSessionState method:
[Microsoft.Online.Administration.Automation.ConnectMsolService]::ClearUserSessionState()
You’ll see in the following input, how the connection has been closed by the .Net function:
PS C:\> Connect-MsolService PS C:\> get-msoluser -UserPrincipalName tycho.brahe@azure.skrubbeltrang.com UserPrincipalName DisplayName isLicensed ----------------- ----------- ---------- tycho.brahe@azure.skrubbeltrang.com Tycho Brahe False PS C:\> [Microsoft.Online.Administration.Automation.ConnectMsolService]::ClearUserSessionState() PS C:\> get-msoluser -UserPrincipalName tycho.brahe@azure.skrubbeltrang.com get-msoluser : You must call the Connect-MsolService cmdlet before calling any other cmdlets. At line:1 char:1 + get-msoluser -UserPrincipalName tycho.brahe@azure.skrubbeltrang.com + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [Get-MsolUser], MicrosoftOnlineException + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.MicrosoftOnlineException,Microsoft.Online.Administration.Automation.GetUser
Remove You On-Premises Exchange Server
In the first half of 2021, the Hafnium zero-day exploit compromised hundreds of thousands of on-premises Exchange Servers worldwide.
Then again, in November 2021, a new security hole was discovered in Exchange on-premises.
Removing Exchange on-premises is the only way to get fully protected from these attacks (and save time and money)!
If you have fully migrated your mailboxes to Office 365, you should have a look at Easy365Manager.
Easy365Manager is a snap-in for Active Directory Users & Computers. With Easy365Manager, you can manage email attributes, Office 365 licenses, and Office 365 mailboxes directly from user and group properties in AD Users & Computers.
With Easy365Manager, you’ll find the following new tabs available in user properties in AD Users & Computers:


With the new tabs, you no longer need to switch between multiple web consoles to manage your users and groups.
You can use AD Users & Computers to do all kinds of Office 365 management, e.g., assign Office 365 licenses to your users:

You can download a fully functional 30-day trial here.