How can I run SharePoint Online PowerShell from Azure Cloud Shell?

James Slora 41 Reputation points
2024-06-03T13:12:09.2366667+00:00

I want to run SharePoint Online PowerShell, from Azure Cloud Shell. It yields nonsense about the command being unrecognized, though. Here is some example session transcription.

PS /home/entra> Connect-SPOService -Url https://(tenant-admin).sharepoint.com

Connect-SPOService: The term 'Connect-SPOService' is not recognized as a name of a cmdlet, function, script file, or executable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

PS /home/entra> Get-Module -ListAvailable -Name Microsoft.Online.SharePoint.PowerShell | Select Name,Version

Name Version


Microsoft.Online.SharePoint.PowerShell 16.0.24810.12000

PS /home/entra> Connect-SPOService -Url https://(tenant-admin).sharepoint.com

Connect-SPOService: The term 'Connect-SPOService' is not recognized as a name of a cmdlet, function, script file, or executable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

PS /home/entra> Uninstall-Module -Name Microsoft.Online.SharePoint.PowerShell -AllVersions -Force

PS /home/entra> Install-Module Microsoft.Online.SharePoint.PowerShell

Untrusted repository

You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?

[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A

PS /home/entra> Connect-SPOService -Url https://(tenant-admin).sharepoint.com

Connect-SPOService: The term 'Connect-SPOService' is not recognized as a name of a cmdlet, function, script file, or executable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

PS /home/entra> Update-Module -Name Microsoft.Online.SharePoint.PowerShell

PS /home/entra> Connect-SPOService -Url https://(tenant-admin).sharepoint.com

Connect-SPOService: The term 'Connect-SPOService' is not recognized as a name of a cmdlet, function, script file, or executable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

PS /home/entra>

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Yuki Sasaki 80 Reputation points
    2024-10-31T12:47:18.8+00:00

    @James Slora Hello! I was in the same boat - I followed the same steps as William provided, but got the Object reference not set to an instance of an object error in the end.

    Unfortunately, Connect-SPOService is not working properly on Azure Cloud Shell due to what seems to be a bug. Many users have reported similar issues on GitHub (https://github.com/SharePoint/sp-dev-docs/issues/9434), but it's not fixed yet (as of writing). Apparently, Mac and Linux PowerShell instances, in addition to Azure Cloud Shell, are affected.

    As a workaround, you can use a Windows PC (if you have one) or spin up a Windows Server VM on Azure, since the Windows version of the command line tool is not affected. (The latter is the approach I eventually took after struggling with the error on Azure Cloud Shell and Ubuntu pwsh for two hours.)

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. William Nieto 545 Reputation points
    2024-06-03T13:48:16.84+00:00

    It seems you're encountering issues while running SharePoint Online PowerShell commands from Azure Cloud Shell.

    Install the SharePoint Online Management Shell:

    • First, uninstall any existing version:
       powershellCopy code
       Uninstall-Module -Name Microsoft.Online.SharePoint.PowerShell -AllVersions -Force
    
      - Then, install the module:
      
      ```powershell
      powershellCopy code
      Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force -AllowClobber
      ```
      
         - If you don't have administrative privileges, install the module for the current user:
         
         ```powershell
         powershellCopy code
         Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser
         ```
         
         **Import the Module**:
         
            - Import the module explicitly to ensure it's loaded:
            
            ```yaml
            powershellCopy code
            Import-Module Microsoft.Online.SharePoint.PowerShell
            ```
            
            **Set Execution Policy**:
            
               - Set the execution policy to allow running scripts:
               
               ```yaml
               powershellCopy code
               Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
               ```
               
               **Connect to SharePoint Online**:
               
                  - Use the **`Connect-SPOService`** cmdlet:
                  
                  ```yaml
                  powershellCopy code
                  Connect-SPOService -Url https://yourtenant-admin.sharepoint.com
                  ```
                  
                     - Replace **`yourtenant-admin`** with your actual tenant admin URL.
                     
                     **Verify Your Connection**:
                     
                        - After connecting, you should be able to run SharePoint Online cmdlets successfully.
                        
    

    Here’s a summary of the commands you'll need:

    powershellCopy code
    Uninstall-Module -Name Microsoft.Online.SharePoint.PowerShell -AllVersions -Force
    Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force -AllowClobber
    Import-Module Microsoft.Online.SharePoint.PowerShell
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
    Connect-SPOService -Url https://yourtenant-admin.sharepoint.com
    

    Remember to replace placeholders like yourtenant-admin.sharepoint.com with your actual SharePoint admin center URL. For more detailed information official documentation.


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.