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

Aathira picture Aathira · Apr 6, 2017 · Viewed 126.1k times · Source
        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.

Answer

Anthony O. picture Anthony O. · Jul 30, 2018

I had to do this in that order:

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