Connect to SharePoint Online Using PowerShell [2-Ways]

You can connect with Sharepoint Online either using SharePoint Online PowerShell module or PnP.PowerShell module. In this blog post, we will explore both these methods and establish a connection with SharePoint Online.

Option 1: Connect to SharePoint Online using Connect-SPOService

You can download and install SharePoint Online Management Shell on your computer or use Install-Module cmdlet to install it directly from PowerShell console. After it’s installed, search for the module in Start menu and run as administrator.

Install Sharepoint Online Management Shell

You don’t have to download and Install SharePoint Online Management Shell, you can directly install the module from a PowerShell console using below commands:

Check if Sharepoint Online PowerShell Module is Installed

Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select Name,Version

Install Sharepoint Online PowerShell module

Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force -AllowClobber

If you are unable to open the PowerShell console as an administrator, install the module using the following PowerShell command on a PowerShell console open in a standard user context.

Install Sharepoint Online PowerShell module (If you are not an administrator)

Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force -AllowClobber -Scope CurrentUser

Get Sharepoint online admin URL. You can find the URL in the browser’s address bar of the SharePoint admin portal.

Sharepoint Online admin url

Connect to Sharepoint Online

Connect-SPOService -url <sharepoint site admin URL>

Example: 
Connect-SPOService -url https://mylab000-admin.sharepoint.com 

Option 2: Connect to Sharepoint Online Using Connect-PnPOnline

You can also use PnP PowerShell module for establishing connection with SharePoint Online. For connecting with SharePoint Online using Connect-PnPOnline, you will need to create an app registration in Entra ID and use it to establish a connection. Refer to the Step-by-step guide for more details: Connect to Sharepoint Online using PowerShell and Entra App or use the link Connect using Connect-PnPOnline.

PnP.Powershell modules no longer works on Powershell 5.1 version. You will need to Install Powershell 7.2 or later version to work with this module. Refer to this link for downloading and Installing Powershell 7.x.

  • Open Powershell 7.2 or later console as an administrator and execute below command.

Install PnP.Powershell Module

Install-Module -Name PnP.PowerShell -Force -AllowClobber

Connect to Sharepoint Online [ More Info ]

$props= @{
    ClientId             = "<ClientID>"  
    CertificatePath      = "<CertificatePath>"  
    CertificatePassword  = (ConvertTo-SecureString -AsPlainText "<Password>" -Force)  
    Url                  = "https://<SharepointSite URL>"  
    Tenant               = "<Tenant Name>" 
}

Connect-PnPOnline @props

Troubleshooting Common Errors

Connect-SPOService: The term ‘Connect-SPOService’ 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.

If you encounter this error message, it indicates that the SharePoint Online PowerShell module is not installed on your device. You can either download and install SharePoint Online Management Shell or use the Install-Module -Name Microsoft.Online.SharePoint.PowerShell command to install SharePoint Online Management Shell directly from PowerShell. After installing the module, you should be able to run the Connect-SPOService cmdlet successfully.

Files cannot be loaded because running scripts is disabled on this system.

If you encounter the Powershell Running Scripts Is Disabled On This System error while running PowerShell scripts, you can follow this link for instructions on how to fix this error: Fix: Running Scripts is disabled on this System Error.

Read Next

Leave a Comment