Read time: 8 minutes

Summary: To connect to SharePoint Online using PowerShell, download and install the SharePoint Online Management Shell module and then run the Connect-SPOService cmdlet with your SharePoint admin center URL. After that, sign in with an account that has the required SharePoint Administrator or Global Administrator permissions and complete any MFA verification. You can also use the community-driven PnP PowerShell module (for site-level and list operations) to do the same with the administrative permissions.

In Microsoft 365 SharePoint, you may need to check several SharePoint sites, review their settings, or perform the same administrative task across multiple site collections. To do such tasks through the interface, you can easily imagine how long it may take and how cumbersome it is with the SharePoint admin center.

However, you can use PowerShell to automate and handle many of these tasks more directly. Instead of repeating the same clicks, you can run commands to retrieve information and manage supported settings, along with automating routine work. To learn how to connect to SharePoint Online using PowerShell, PnP PowerShell, and Graph PowerShell, and verify the connection as well, stay with us here.

Note: Before you start, understand that not every PowerShell command for SharePoint belongs to the same tool. The SharePoint Online Management Shell, Microsoft Graph PowerShell, and PnP PowerShell have different purposes.

What is SharePoint Online Management Shell?

SharePoint Online Management Shell is Microsoft’s PowerShell module for administering SharePoint Online. It provides a set of cmdlets for supported tenant-level administrative tasks, including managing site collections and configuring SharePoint settings. The main connection command is Connect-SPOService. It connects an admin to the SharePoint admin center and allows them to run supported SharePoint Online cmdlets to get the job done. However, you must disconnect PowerShell from the admin center once you have performed what you want. For that, Disconnect-SPOService is the needed command.

Read More: SharePoint 2016 vs SharePoint 2019 – A Brief Comparison

Prerequisites for Connecting to SharePoint Online via PowerShell

Before running the commands to connect to SharePoint Online, make sure you have:

  • A Microsoft 365 account that can sign in to your organization’s tenant
  • The required SharePoint admin role, such as SharePoint Admin or Global Admin role
  • Correct SharePoint admin center URL (https://yourtenant-admin.sharepoint.com)
  • The SharePoint Online Management Shell module installed on your computer
  • An authentication method that complies with your organization’s security policies

Step-by-Step Process: Connect to SharePoint Online Using PowerShell

Here is the complete set of commands from installation to connection and disconnection. Understand them and execute them to connect to SharePoint Online PowerShell:

Step 1: Open PowerShell and run the command below to check your PowerShell version

$PSVersionTable.PSVersion

Step 2: To install the SharePoint Online Management Shell module, run this command:

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

If PowerShell asks you to approve installation from the PowerShell Gallery, verify the repository before proceeding.

Note: If you already have the PowerShell module installed, check its available versions:
Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable
To update an existing installation, run:
Update-Module -Name Microsoft.Online.SharePoint.PowerShell
If the module is not installed, use the installation command instead.

Step 3: Now run the command below to connect to the SharePoint admin center:

Connect-SPOService -Url https://YourTenant-admin.sharepoint.com

Replace the sample URL with your actual SharePoint admin center URL.Replace the URL with Admin Center URL

Step 4: PowerShell will ask you to sign in. Enter your Microsoft 365 account details and complete any additional authentication steps, such as MFA.Enter your account details

Note: If you manage multiple Microsoft 365 tenants, double-check the URL before connecting. Otherwise, you could run a command against the wrong tenant.

Step 5: After signing in, run a read-only command to verify the connection:

Get-SPOSite -Limit 5
Run a read only command first

Step 6: Once connected, you can run supported SharePoint Online Management Shell cmdlets.Run your SharePoint Online Management Shell cmdlets after getting connected

  • Get-SPOSite: It will retrieve site collection information.
  • Set-SPOHomeSite: It will set the SharePoint Site as the Home Site.
  • Set-SPOTenant: It will set the properties on the SharePoint Online tenant.
  • Set-SPOUser: It will set various properties on an active user.
  • Test-SPOSite: It will evaluate the SharePoint Online site collection.
  • Upgrade-SPOSite: It will initialize an upgrade process for the SharePoint Online site collections.
  • Export-SPOUserProfile: It will export the selected user profile to a CSV file.
  • Get-SPOAppErrors: If there is an error in the SharePoint Online account, it will show it.
  • Enable-SPOCommSite: The command creates a communication site experience on a previously existing classic team site.
  • New-SPOSiteGroup: It will create a new site group for the SharePoint Online account collection.
  • Remove-SPODeletedSite: The command will remove a SharePoint Site by deleting it from the recycle bin.
  • Add-SPOSiteDesign: It will create a new site design for the users when they create a new site.
  • Repair-SPOSite: It will check the sites and repair minor issues.
  • Disconnect-SPOService: It will disconnect the SharePoint Online Service.

Use the relevant cmdlet for the task you need to complete, and review its parameters before changing tenant settings or site configurations.

Step 7: When you are set to finish, you can close the PowerShell session. Run the command below:

Disconnect-SPOService

How to Connect to SharePoint Online using PnP PowerShell

For site and data management, PnP (Patterns and Practices) PowerShell can also be used to connect to SharePoint Online. It is highly popular because it allows you to connect directly to specific site collections. Here are the steps and commands to follow:

Step 1: Open PowerShell and run the command below to install the PnP Module:

Install-Module -Name PnP.PowerShell -Scope CurrentUser

Step 2: Now run the command below to connect to a specific SharePoint Site

Connect-PnPOnline -Url “https://YourTenant-admin.sharepoint.com” -Interactive

The –Interactive switch triggers a modern authentication web browser pop-up that supports MFA. Input your credentials and confirm the connection.

Step 3: Verify the Connection by fetching the lists inside that specific site using the command below:

Get-PnPList

Can I Connect to SharePoint Online Using Modern Authentication?

Yes. You can and must connect to SharePoint Online using modern authentication (OAuth and OpenID Connect). This is because Microsoft has permanently blocked legacy client and basic authentication protocols.

While connecting, admins may encounter the error Could not connect to SharePoint Online even when the admin URL is correct. For this, there can be a problem with authentication configuration that blocks the authentication flow being used. To get rid of this, make sure that your installed module version supports the required parameters. For an applicable federated authentication scenario, the connection can use:

$creds = Get-Credential
Connect-SPOService -Url “https://YourTenan-admin.sharepoint.com” -ModernAuth $true -AuthenticationUrl “https://login.microsoftonline.com/organizations”

Replace the example URL with your tenant’s admin center URL.

The -ModernAuth and -AuthenticationUrl parameters should be used when appropriate for your environment.

You can also try the system-browser authentication option if your installed module supports it:

Connect-SPOService `
-Url https://YourTenant-admin.sharepoint.com
-UseSystemBrowser $true

This uses the system browser for the sign-in process. You must still meet your organization’s authentication and access requirements.

Can I Connect to SharePoint Online Using Microsoft Graph PowerShell

Yes, you can connect to SharePoint Online using Microsoft Graph PowerShell. It is another option when your task involves resources exposed through Microsoft Graph APIs. Here are the cmdlets to connect to SharePoint Online using Microsoft Graph PowerShell:

Step 1: Run the command below to install the Graph PowerShell SDK:

Install-Module Microsoft.Graph -Scope CurrentUser

Step 2: Connect with the required delegated permission:

Connect-MgGraph -Scopes “Sites.Read.All”

Step 3: After that, complete the sign-in and consent process required by your organization’s policies.

Once connected successfully, you can use supported Graph cmdlets to work with SharePoint resources.

Note: Use Graph PowerShell only when the operation is supported by Microsoft Graph and its permission model fits your requirements. Use the SharePoint Online Management Shell for supported SharePoint tenant administration tasks that are exposed through its cmdlets.
Recommendation: You may need to perform a migration when moving from one SharePoint Site to another, moving to Microsoft 365, consolidating tenants, or transferring files from another platform. To complete such tasks smoothly with full data integrity, Kernel Migrator for SharePoint is one option to get help. It can help migrate SharePoint content and preserve metadata, permissions, and version history as well.

Final Thoughts

We hope you now understand how to connect to SharePoint Online using PowerShell cmdlets. It becomes simpler if you have all prerequisites, the correct module, the admin center URL, and needed permissions. With SharePoint Online Management Shell, you can not only manage the existing accounts and sites, but also create more site collections, assign permissions, migrate site collections, and delete various parts.

When planning for tenant consolidation, business merger, department separation, or such other tasks, migrating SharePoint Online via PowerShell can be complex. Moreover, a SharePoint migration tool can reduce the hassle of exporting data from one tenant to another.

Kernel Migrator for SharePoint
Related Posts
Google Trust