[powershell] 'Connect-MsolService' is not recognized as the name of a cmdlet

PSCommand commandToRun = new PSCommand();
commandToRun.AddCommand("Connect-MsolService");
commandToRun.AddParameter("Credential", new PSCredential(msolUsername, msolPassword));

powershell.Streams.ClearStreams();
powershell.Commands = commandToRun;
powershell.Invoke();

I am trying to run above code in visual studio and getting the following error : The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. But I am able to Connect to Msol service from Microsoft Azure Active Directory Module for Windows PowerShell. Please help.

This question is related to powershell azure office365

The answer is


I had to do this in that order:

Install-Module MSOnline
Install-Module AzureAD
Import-Module AzureAD

This issue can occur if the Azure Active Directory Module for Windows PowerShell isn't loaded correctly.

To resolve this issue, follow these steps.
1.Install the Azure Active Directory Module for Windows PowerShell on the computer (if it isn't already installed). To install the Azure Active Directory Module for Windows PowerShell, go to the following Microsoft website:
Manage Azure AD using Windows PowerShell

2.If the MSOnline module isn't present, use Windows PowerShell to import the MSOnline module.

Import-Module MSOnline 

After it complete, we can use this command to check it.

PS C:\Users> Get-Module -ListAvailable -Name MSOnline*


    Directory: C:\windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.1.166.0  MSOnline                            {Get-MsolDevice, Remove-MsolDevice, Enable-MsolDevice, Disable-MsolDevice...}
Manifest   1.1.166.0  MSOnlineExtended                    {Get-MsolDevice, Remove-MsolDevice, Enable-MsolDevice, Disable-MsolDevice...}

More information about this issue, please refer to it.


Update:

We should import azure AD powershell to VS 2015, we can add tool and select Azure AD powershell.

enter image description here


All links to the Azure Active Directory Connection page now seem to be invalid.

I had an older version of Azure AD installed too, this is what worked for me. Install this.

Run these in an elevated PS session:

uninstall-module AzureAD  # this may or may not be needed
install-module AzureAD
install-module AzureADPreview
install-module MSOnline

I was then able to log in and run what I needed.


Examples related to powershell

Why powershell does not run Angular commands? How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line? How to print environment variables to the console in PowerShell? Check if a string is not NULL or EMPTY The term 'ng' is not recognized as the name of a cmdlet VSCode Change Default Terminal 'Connect-MsolService' is not recognized as the name of a cmdlet Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet Change directory in PowerShell

Examples related to azure

How is VIP swapping + CNAMEs better than IP swapping + A records? 'Connect-MsolService' is not recognized as the name of a cmdlet Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" How to view the roles and permissions granted to any database user in Azure SQL server instance? How to get the azure account tenant Id? Azure SQL Database "DTU percentage" metric Could not load file or assembly System.Web.Http.WebHost after published to Azure web site How to re-create database for Entity Framework? How do I create a new user in a SQL Azure database?

Examples related to office365

'Connect-MsolService' is not recognized as the name of a cmdlet Send SMTP email using System.Net.Mail via Exchange Online (Office 365)