15
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.

1
  • 3
    try to import the module using Import-Module MSOnline Commented Apr 6, 2017 at 10:47

4 Answers 4

22

I had to do this in that order:

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

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.

4

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

7
  • It is not working.. I am able to run the powershell MSOL commands using my AAD console but its throwing error while I try to debug it using visual studio(2015)
    – Aathira
    Commented Apr 7, 2017 at 23:36
  • Jason Ye -Could you please let me know if there is some other work around that you might be knowing?
    – Aathira
    Commented Apr 9, 2017 at 1:38
  • Additional Info: PSVersion 4.0. I have copied the MSOnline and MSOnlineExtented files from System32/WindowsPowerShell/v1.0/Modules to SysWOW64/WindowsPowerShell/v1.0/Modules.. But now I am getting another error like this : Automation.PSModule.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
    – Aathira
    Commented Apr 10, 2017 at 13:20
  • Sorry, I am checking on the query and would get back to you soon on this.
    – Jason Ye
    Commented Apr 11, 2017 at 5:57
  • @Aathira It seems we should import azure ad powershell to this tool, I have update my answer, please check it.
    – Jason Ye
    Commented Apr 11, 2017 at 9:28
3

Following worked for me:

  1. Uninstall the previously installed ‘Microsoft Online Service Sign-in Assistant’ and ‘Windows Azure Active Directory Module for Windows PowerShell’.
  2. Install 64-bit versions of ‘Microsoft Online Service Sign-in Assistant’ and ‘Windows Azure Active Directory Module for Windows PowerShell’. https://littletalk.wordpress.com/2013/09/23/install-and-configure-the-office-365-powershell-cmdlets/

If you get the following error In order to install Windows Azure Active Directory Module for Windows PowerShell, you must have Microsoft Online Services Sign-In Assistant version 7.0 or greater installed on this computer, then install the Microsoft Online Services Sign-In Assistant for IT Professionals BETA: http://www.microsoft.com/en-us/download/details.aspx?id=39267

  1. Copy the folders called MSOnline and MSOnline Extended from the source

C:\Windows\System32\WindowsPowerShell\v1.0\Modules\

to the folder

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\

https://stackoverflow.com/a/16018733/5810078.

(But I have actually copied all the possible files from

C:\Windows\System32\WindowsPowerShell\v1.0\

to

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\

(For copying you need to alter the security permissions of that folder))

Not the answer you're looking for? Browse other questions tagged or ask your own question.