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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Aathira
  • 655
  • 3
  • 14
  • 31

4 Answers4

21

I had to do this in that order:

Install-Module MSOnline
Install-Module AzureAD
Import-Module AzureAD
Anthony O.
  • 22,041
  • 18
  • 107
  • 163
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.

Community
  • 1
  • 1
Basher 590
  • 166
  • 1
  • 4
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

Jason Ye
  • 13,710
  • 2
  • 16
  • 25
  • 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 Apr 07 '17 at 23:36
  • Jason Ye -Could you please let me know if there is some other work around that you might be knowing? – Aathira Apr 09 '17 at 01: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 Apr 10 '17 at 13:20
  • Sorry, I am checking on the query and would get back to you soon on this. – Jason Ye Apr 11 '17 at 05:57
  • @Aathira It seems we should import azure ad powershell to this tool, I have update my answer, please check it. – Jason Ye Apr 11 '17 at 09:28
  • @JasonThanks for the reply, but i have got another work around and posted the same as answer. – Aathira Apr 11 '17 at 11:56
  • Link isnt working – Jeppe Sep 15 '22 at 09:41
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))

Community
  • 1
  • 1
Aathira
  • 655
  • 3
  • 14
  • 31