Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The term 'Connect-AzureAD' is not recognized as the name of a cmdlet

Running powershell script from C# application in Azure AD.

Added below DLL reference

  • System.Management.Automation
  • Microsoft.Online.Administration.Automation.PSModule.Resources
  • Microsoft.Online.Administration.Automation.PSModule

Runspace runspace = RunspaceFactory.CreateRunspace();
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript("Import-Module AzureAD -Force;");
                pipeline.Commands.AddScript("$password = ConvertTo-SecureString " + "\"abc1234\"" + " -AsPlainText -Force");
                pipeline.Commands.AddScript("$Cred = New-Object System.Management.Automation.PSCredential (" + "\"[email protected]\"" + ", $password)");
                pipeline.Commands.AddScript("Connect-AzureAD -Credential $Cred");
                pipeline.Commands.AddScript("Get-AzureADApplication -Filter " + "\"DisplayName eq " + "\'PortalTestApp\'" + "\"");
                var result = pipeline.Invoke();

Getting Error:

The term 'Connect-AzureAD' 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.

like image 606
user1638526 Avatar asked Jun 11 '19 12:06

user1638526


People also ask

How do I connect to AzureAD in PowerShell?

Type “PowerShell” from the start menu >> Right-click on Windows PowerShell and choose “Run as administrator” Type “Install-Module AzureAD” and hit Enter. You'll be asked to confirm the installation from the PSGallery. Type “A” to select “Yes to All” and hit the Enter key.

How do you fix the term is not recognized as the name of a cmdlet?

If that module is missing, corrupt, or got moved, it throws up the error, “the term is not recognized as the name of a cmdlet.” You can use “get-module” in PowerShell to see if the module is present and correct. It will show you what modules are loaded, and you can add or repair them depending on your needs.

How do I install an AzureAD module in PowerShell?

Follow these steps to install the Microsoft Azure Active Directory Module for Windows PowerShell: Open an elevated Windows PowerShell command prompt (run Windows PowerShell as an administrator). Run the Install-Module MSOnline command. If you're prompted to install the NuGet provider, type Y and press Enter.


1 Answers

I had an issue with PowerShell v7, unlike PS v5, you have to import the module after installation with Import-Module AzureAD. The error is identical to if you haven't imported it after installing it from a module source like PSGallery.

like image 177
Shadoweb Avatar answered Sep 19 '22 08:09

Shadoweb