The Term Connect-ExchangeOnline is not recognized name of cmdlet, function, script file or operable program

Amitesh Sharma 1 Reputation point
2022-11-14T20:19:06.263+00:00

I am trying to connect with office365 PowerShell. I had also imported the module "ExchangeOnlineManagement". I'm getting the below exception:

"The term Connect-ExchangeOnline is not recognized as cmdlet, function , script or operable problem".

string script = @"  
               Set-ExecutionPolicy Unrestricted  
               $user = '<your username>'  
               $pwd = '<your password>'  
               $SecurePass = ConvertTo-SecureString -AsPlainText $pwd -Force  
               $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $user,$SecurePass  
               Import-Module -Name ExchangeOnlineManagement  
               Connect-ExchangeOnline -Credential $Cred  
               ";  
try  
{  
   using (Runspace runspace = RunspaceFactory.CreateRunspace())  
   {  
      runspace.Open();  
      Pipeline pipe = runspace.CreatePipeline();  
      pipe.Commands.AddScript(script);  
        
      try  
      {  
         var results = pipe.Invoke();  
      }   
      catch (Exception e)  
      {  
  
      }  
        
      var error = pipe.Error.ReadToEnd();  
      if (error.Count > 0)  
      {  
         foreach (PSObject err in error)  
         {  
            //more logging not sharing that code  
         }  
      }  
   }  
}  
catch (Exception ex)  
{ }  
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hoekstra Jelle 501 Reputation points
    2022-11-16T14:22:18.963+00:00

    Hi!

    The question remaining is did the import of the module go well.
    (Like could the module be loaded in full)

    I would recommend to add the following line above the line where you import the module.
    Install-Module -Name ExchangeOnlineManagement -Force

    Alternatively you could do something with try-catch but I am not sure if that works in this scenario.

    Hope it helps!

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.