How to Fix the Connect-MsolService Error in PowerShell 7
Question details
The user needs to resolve a type-loading error that occurs when executing the Connect-MsolService cmdlet in PowerShell 7.

- Product
- PowerShell / MSOnline Module
- Device & OS
- not provided
- Scenario
- Attempting to authenticate and connect to Microsoft Online Services using PowerShell 7.
- Observed behavior
- The MSOnline module imports successfully, but running Connect-MsolService triggers a System.Security.Cryptography.SHA256Cng type-loading error.
Check your current PowerShell version by typing '$PSVersionTable.PSVersion' in your console to confirm if you are running PowerShell 7 or later.
Switch to Windows PowerShell 5.1
Use the legacy Windows PowerShell 5.1 environment, which natively supports the MSOnline module and its dependencies.
The MSOnline module relies on .NET Framework components that are not present in the modern .NET architecture used by PowerShell 7. To run these legacy cmdlets successfully, you must use Windows PowerShell 5.1.
Click the Start button, type 'Windows PowerShell' (ensure it does not say PowerShell 7), and select 'Run as Administrator'.
In the console, execute 'Import-Module MSOnline' to load the required cmdlets into your session.
Type 'Connect-MsolService' and press Enter. A prompt will appear allowing you to enter your Microsoft 365 credentials.

Migrate to the Microsoft Graph PowerShell SDK
Upgrade your scripts to use the Microsoft Graph PowerShell SDK, which is fully supported in PowerShell 7 and later.
Boost Your Productivity with WPS Office
If you frequently manage IT environments, scripts, and documentation, having a reliable office suite is crucial. WPS Office is a highly compatible, lightweight alternative to Microsoft Office that allows you to easily edit, format, and share your technical documents for free.
- 1. Download the Installer: Visit the official WPS Office website and click the download button for your operating system.
- 2. Install the Software: Run the downloaded installer and follow the on-screen instructions to complete the setup.
- 3. Start Creating Documents: Open WPS Office and instantly start drafting scripts, reports, or data sheets with full Microsoft format support.

Frequently Asked Questions
Why does Connect-MsolService fail specifically in PowerShell 7?
PowerShell 7 is built on modern .NET Core, whereas the MSOnline module relies on legacy .NET Framework cryptography libraries (such as SHA256Cng). This architectural difference causes the type-loading failure when executing the connection cmdlet.
Can I force the MSOnline module to work in PowerShell 7?
No. Although PowerShell 7 features a compatibility layer for some older modules, the specific authentication mechanisms in Connect-MsolService cannot be forced to run. You must fall back to Windows PowerShell 5.1.
What is the replacement for the MSOnline PowerShell module?
Microsoft has designated the Microsoft Graph PowerShell SDK as the official replacement for both the MSOnline and Azure AD modules. It is fully supported in modern PowerShell versions.




