fbpx
Active Directory & Office 365 Reporting Tool

Fix – Connect-MsolService Not Recognized (How to Fix). Did you receive the “The term ‘Connect-MsolService’ is not recognized as the name of a cmdlet, function, script file, or operable program” error when you run the Connect-MsolService command? There is a simple fix for this error, so please continue with  Fix – Connect-MsolService Not Recognized (How to Fix).

But what causes the error message? The likely reason for this error is that you’ve not installed the MSOnline Azure Active Directory PowerShell module. The Connect-MsolService command is part of the MSOnline module.

So, if you do not have this module on your computer, you will receive the “Connect-MsolService not recognized” error. However, if you previously installed the MSOnline PowerShell module but received this error message, the module may be corrupt. 

Follow the steps in the sections below to resolve the error message. 

Method 1 Fix for the "Connect-MsolService Not Recognized" Error Message

If you haven’t previously installed the MSOnline module, here are the steps to install and fix the error message.

Open PowerShell as Administrator and Modify the Execution Policy

1. Search “powershell,” open the app as admin by clicking Run as Administrator. Windows displays a prompt asking you to authorize the app to make changes to your device – click Yes.

By default Windows PCs do not permit running downloaded PowerShell modules. To modify this policy, run the command below. 

				
					powershell.exe -ExecutionPolicy RemoteSigned
				
			

The command opens a new instance of PowerShell with the RemoteSigned Execution Policy. All Microsoft PowerShell modules are signed, so this policy should allow the commands in the MSOnline module to run. 

2. The next step is to confirm that the MSOnline is not installed on your PC. Checking, if it exists on your computer is a good idea before installing the module.

Run the command to verify if the MSOnline is on your computer. 

				
					Import-Module MSOnline
				
			

If the command returns the error message “The specified module ‘MSOnline’ was not loaded because no valid module file was found in any module directory,” the module is not installed on your computer.

In that case, proceed to the following sub-section below to install the module. Otherwise, if the command did not return any errors, but you still get errors when you run the Connect-MsolService command, it may indicate that the module is corrupted. 

If you find yourself in this situation, please proceed to the method 2 of this article.

We are going through first method of Fix – Connect-MolService Not Recognized (How to Fix). Let’s read it and continue through to the method 2. 

Install the MSOnline Module to Fix the "Connect-MsolService Not Recognized" Error

To install the MSOnline PowerShell module, run the command below from a PowerShell console you opened as admin. 

				
					Install-Module -Name MSOnline
				
			

After installing, PowerShell reverts to its command prompt.

Confirm that the installation was successful and that the MSOnline module is available on your computer. To do so, add the module to your current PowerShell session by running the Import-Module command below.

				
					Import-Module MSOnline
				
			

The command executes successfully without errors. This indicates that you now have the MSOnline module.

Regardless, to confirm that it is, run this command. 

				
					Get-Command -Module MSOnline
				
			

The Get-Command command returns all cmdlets available in the MSOnline, including the Connect-MsolService command. 

But, since the Connect-MsolService may be lost in the result, if you want to confirm that it is available, modify the above command to include the Name parameter as shown below. 

				
					Get-Command -Module MSOnline -Name Connect-MsolService
				
			

Run the Connect-MsolService Command without Errors

After you have installed the MSOnline PowerShell module and confirmed that the Connect-MsolService cmdlet is now available on your computer, run the command successfully without the “not recognized” error message!

				
					Connect-MsolService
				
			

When you execute the Connect-MsolService command, PowerShell prompts you to enter your Office 365 sign in email address. Enter the email address and click Next. 

Next, enter your Office 365 password and click Sign in. 

If PowerShell returns without error, you have successfully connected to your Office 365 tenant with the Connect-MsolService command. Now run available commands. 

I have run the Get-MsolDomain command to demonstrate an example – see the screenshot below. 

Try our Active Directory & Office 365 Reporting & Auditing Tools

Try us out for Free.  100’s of report templates available. Easily customise your own reports on AD, Azure AD & Office 355.

Method 2 Fix for the "Connect-MsolService Not Recognized" Error Message

In the introduction of this article, I stated that the “The term ‘Connect-MsolService’ is not recognized as the name of a cmdlet, function, script file, or operable program” error is commonly caused by the MSOnline module’s absence. Additionally, I mentioned that this error message might also appear, if the installation of your MSOnline module is corrupt.

This section of Fix – Connect-MsolService Not Recognized (How to Fix) discusses fixing this error, if you have installed the MSOnline but still receive the “Connect-MsolService not recognized” error message. 

Uninstall and Delete All MSOnline Modules

If you suspect your computer’s MSOnline PowerShell module is broken, run the Uninstall-Module command below. 

Before you run the command, follow the steps “Open PowerShell as Administrator and Modify the Execution Policy” sub-session in the method 1 section of this article. 

				
					Uninstall-Module -Name MSOnline -Force
				
			

After you run the above command to uninstall the MSOnline module, next is to search for and delete all the folders for the module. To find and remove all the module folders for MSOnline, run the script below from a PowerShell console you opened as admin. 

Preferably, copy the script to a PowerShell ISE document and run it from there. 

				
					$PSModulePaths = $Env:PSModulePath -split ";"
ForEach ($PSModulePath in $PSModulePaths)  {
Get-ChildItem -Path $PSModulePath -Recurse -Directory -Filter "MSOnline" | Remove-Item -Force -Recurse 
}
				
			

Finally, reinstall the MSOnline PowerShell module by running the following commands (one line at a time). 

				
					Install-Module -Name MSOnline
Import-Module MSOnline
Get-Command -Module MSOnline -Name Connect-MsolService
				
			

The first and second commands install and import the module to your computer. Then, the second command confirms that the Connect-MsolService command is now available. 

If the last command returns a result, use the Connect-MsolService command to connect to your Office 365 tenant without receiving the “Connect-MsolService not recognized” error. 

Thank you for reading Fix – Connect-MsolService Not Recognized (How to Fix). We conclude the article now. 

Fix - Connect-MsolService Not Recognized (How to Fix) Conclusion

The leading cause of the “Connect-MsolService Not Recognized” error message is typically the lack of the MSOnline PowerShell module installed on the computer. If you encounter this error message, confirm whether the module is installed by running the “Import-Module MSOnline” command.

In case the command returns an error, it suggests that the module is not installed, resolve it by running the “Install-Module -Name MSOnline” command.

After the installation, proceed to import the MSOnline module to your current PowerShell session by rerunning the “Import-Module MSOnline” command.

If you encounter no errors when first running the “Import-Module MSOnline” command, it could imply that the installed module is corrupted. In such cases, remove the module by running the “Uninstall-Module -Name MSOnline -Force” command, followed by using the script in Method 2 of this article to delete the MSOnline module folder from your computer.

Finally, reinstall the module by executing the “Install-Module -Name MSOnline” command.

InfraSOS-AD-Tools

Try InfraSOS for FREE

Invite your team and explore InfraSOS features for free

Victor Ashiedu

Victor Ashiedu

Victor is an IT pro based in Manchester, UK. With over 22 years of experience managing Windows Server, Active Directory, and Powershell, and 7 years of expertise in Azure AD and Office 365, he's a seasoned expert in his field. When he's not working, he loves spending time with his family - a wife and a 5-year-old. Victor is passionate about helping businesses succeed in today's fast-changing tech landscape.

Leave a comment

Your email address will not be published. Required fields are marked *