How to Disconnect From MsolService

How to disconnect from the MsolService with PowerShell

To connect to Azure AD using the PowerShell MS Online module, you use the following command:


So far, so good…

But what if you want to disconnect again?

The Exchange Online PowerShell module has:


The Azure AD PowerShell module has:


But this command is missing from the MS Online PowerShell module.

To disconnect from MS Online, you can use the .Net ClearUserSessionState method:


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

UserPrincipalName                   DisplayName isLicensed
-----------------                   ----------- ---------- Tycho Brahe False

PS C:\> [Microsoft.Online.Administration.Automation.ConnectMsolService]::ClearUserSessionState()
PS C:\> get-msoluser -UserPrincipalName
get-msoluser : You must call the Connect-MsolService cmdlet before calling any other cmdlets.
At line:1 char:1
+ get-msoluser -UserPrincipalName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + 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:

Easy365Manager Office 365 user properties

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:

Create an Office 365 mailbox using Easy365Manager

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

Did you like this post? Maybe your friends will too!