How to Connect to SharePoint Online Using PowerShell

You can manage SharePoint Online using the SharePoint Admin Center, which provides a graphical user interface (GUI) for managing SharePoint sites, lists, documents, and settings. However, it’s important to note that all of these tasks can also be managed using the command line interface (CLI).

Using the command line to manage SharePoint Online is much quicker, especially when dealing with hundreds of SharePoint sites. For example, if you need to disable external sharing on all your SharePoint sites in the organization, using the GUI portal may take a considerable amount of time to complete the task.

You can create a PowerShell script and run it against all your SharePoint sites to disable external sharing. This process will only take a few minutes to complete. Additionally, there are other settings you can manage effortlessly using the command line.

There are different ways to connect to SharePoint Online. You can either use SharePoint Online cmdlets or SharePoint PnP cmdlets to achieve the same goal. SharePoint Online cmdlets will have ‘SPO‘ in the cmdlet name, for example, ‘New-SPOSite‘ and SharePoint PnP cmdlets will have ‘PnP‘ in the name, for example, ‘New-PnPSite.’

Before you can run any SharePoint Online cmdlets, you will need to install the SharePoint Online PowerShell module. Similarly, before running SharePoint PnP cmdlets, you must have the PnP.PowerShell module installed on your device. Let’s check the steps.

Step 1 – Install Sharepoint Online Management Shell

There are two ways to install the SharePoint Online Management Shell Powershell module. The first involves downloading and installing the SharePoint Online Management Shell tool from Microsoft’s website, and the second option is to Install the module directly using the PowerShell console. Let’s explore both options below.

Option 1 – Using Sharepoint Online Management Shell Tool

To install SharePoint Online Management Shell, please follow the steps below:

Install Sharepoint Online Management Shell
Install Sharepoint Online Management Shell
  • Click on Finish to complete the installation process.
Install Sharepoint Online Management Shell
Install Sharepoint Online Management Shell
  • Search for ‘SharePoint Online Management Shell‘ in the Start menu, right-click on it, and select ‘Run as administrator’.
Install Sharepoint Online Management Shell
Install Sharepoint Online Management Shell

Option 2 – Install Sharepoint Online Management Shell Using Powershell

This is an alternative way of installing SharePoint Online Management Shell using PowerShell. Instead of downloading and installing SharePoint Online Management Shell from the Microsoft website, you can open the PowerShell console and run the following commands to install it.

  1. First, confirm if the SharePoint Online PowerShell module is installed on your computer. If it’s not already installed, proceed to the next step to Install it.

Confirm if the Sharepoint Online Powershell Module is already Installed

Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select Name,Version
  1. Install SharePoint Online Management Shell using the following PowerShell command in the PowerShell console (opened as an administrator).

Install Sharepoint Online Powershell module

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

If you are unable to open the PowerShell console as an administrator, install SharePoint Online Management Shell using the following PowerShell command on a non-administrator Powershell console or Current user scope.

Install Sharepoint Online Powershell module (CurrentUser Scope)

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

Step 2 – Connect to Sharepoint Online using Powershell

There are two options to connect with SharePoint Online. The first option is to use the ‘Connect-SPOService‘ cmdlet and the second one is to use Connect-PnPOnline cmdlet. Let’s explore both these options.

Option 1 – Connect to Sharepoint Online Using Connect-SPOService

Before using this command, ensure that the user account intended for connecting to SharePoint Online has either Global Administrator or SharePoint Administrator rights.

  1. Get Sharepoint Online Admin URL
    • Retrieve the admin URL of your SharePoint organization. You can find the URL in the browser’s address bar of the SharePoint admin portal. For my organization, it’s https://mylab000-admin.sharepoint.com.
Sharepoint Online admin url
  1. Connect to Sharepoint Online
    • Use the following commands to Connect to SharePoint online. Replace the URL with your organization’s Sharepoint Admin URL.

Connect to Sharepoint Online

Connect-SPOService -Url https://mylab000-admin.sharepoint.com -credential [email protected]
Connect to Sharepoint online using Connect-SPOService

Option 2 – Connect to Sharepoint Online Using Connect-PnPOnline

There is an alternate cmdlet Connect-PnPOnline which can also be used to connect to Sharepoint Online. SharePoint Patterns and Practices (PnP) PowerShell cmdlets are open-source, and it’s important to note that there is no SLA or direct support from Microsoft for PnP cmdlets.

Before running SharePoint PnP-based cmdlets, you need to install the PnP PowerShell module on your device. Launch the PowerShell console as an administrator and run the following command.

Install PnP.Powershell Module

Install-Module -Name "PnP.PowerShell"

Connect to Sharepoint Online

Connect-PnPOnline -Url "https://mylab000-admin.sharepoint.com" -Interactive

Once connected to SharePoint Online, you can execute SharePoint PnP cmdlets to manage SharePoint Online. For example, if you want to disable external sharing in SharePoint Online, you can use the following cmdlet.

Example: Disable External Sharing in SPO at Org Level

Set-PnPTenant -SharingCapability Disabled

Troubleshooting

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 Management Shell 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.

Conclusion

In this blog post, we’ve explored various ways to connect to SharePoint Online using PowerShell. You can choose between SharePoint Online Management Shell or SharePoint PnP cmdlets to accomplish your tasks. Once connected using either method, you can execute SharePoint PowerShell scripts or use individual commands to make the necessary changes.

Leave a Comment

Discover more from TechPress

Subscribe now to keep reading and get access to the full archive.

Continue reading