How to connect to sharepoint online using powershell

You can manage sharepoint online using sharepoint admin center which is a graphical user interface (GUI) to manage sharepoint sites, lists, documents and sharepoint settings. However, all of it can be managed using command line as well.

Using command line to manage sharepoint online is much quicker specially when you are dealing with 100’s of sharepoint sites. For example if you have to disable external sharing on all of your sharepoint sites in the organization, if you will use GUI portal it may take forever to complete the task.

You can easily create a powershell script and run it against all your sharepoint sites to disable external sharing as an example. This will take few minutes to complete.There are other settings you can easily manage as well using command line.

There are different ways to connect to sharepoint online. You can either use Sharepoint online cmdlets or Sharepoint PnP cmdlets to acheive 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 are able to run any Sharepoint online cmdlets, you will need to Install Sharepoint Online powershell module and before running Sharepoint PnP cmdlets, you will need PnP.Powershell module to be installed on your device.

Install Sharepoint Online Management Shell

To Install Sharepoint Online management shell, please follow below steps:

  • Click on Finish to complete the installation process.
  • Search for Sharepoint Online Management Shell from start menu and right-click on it. Then select Run as administrator.

Installing Sharepoint Online management shell using Powershell console

Instead of downloading and installing sharepoint online management shell on your computer from microsoft website you can also open powershell console and run below commands to install it. Please follow below steps to install sharepoint online management shell directly from Powershell console.

Check to confirm Sharepoint online powershell module is already Installed on your computer.

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

Install Sharepoint online management shell using below powershell command from Powershell console (opened as administrator).

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

Install Sharepoint online management shell using below powershell command from Powershell console (opened as normal user).

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

Connect to Sharepoint online using Connect-SPOService

Next step is to use connect-sposervice cmdlet to connect to sharepoint online. Let’s see how to connect using this cmdlet from Sharepoint online management shell.

Before using this command make sure the user account which you are going to use to connect to sharepoint online must have either Global administrator or sharepoint administrator rights.

Get the admin url of your sharepoint organization. You can find the url from the browser address bar of sharepoint admin portal. For my organization, its https://mylab000-admin.sharepoint.com.

Sharepoint Online admin url
Connect-SPOService -Url https://mylab000-admin.sharepoint.com -credential spoadmin@techpress.net
Connect to Sharepoint online using Connect-SPOService

Connect to Sharepoint online using Connect-PnPOnline

You can manage sharepoint online by connecting to sharepoint online using Connect-SPOservice. But there is another way to connect to sharepoint online which is by using Connect-PnPOnline. SharePoint Patterns and Practices (PnP) Powershell cmdlets are open source cmdlets. There is no SLA or direct support from Microsoft for PnP cmdlets.

Before running sharepoint PnP based cmdlets, you will need to install PnP Powershell module on your device. Launch Powershell console as administrator and run below command. Please make sure Powershell version installed on your device is 5.1 to be able to run Install-module cmdlet.

#Install PnP.Powershell Module
Install-Module -Name "PnP.PowerShell"
#Connect to Sharepoint Online
Connect-PnPOnline -Url "https://mylab000-admin.sharepoint.com" -Interactive

Once you are connected to the sharepoint online, you can run execute Sharepoint PnP cmdlets to manage sharepoint online. For example if you want to disable external sharing in sharepoint online then you can use below cmdlet.

#Disable External Sharing in sharepoint online at organization level
Set-PnPTenant -SharingCapability Disabled

FAQs

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 are getting this error message that means that the Sharepoint online management shell is not installed on your device. You can either download and install Sharepoint online management shell or use 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 Connect-SPOService cmdlet successfully.

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

If you are getting this error message while running Powershell scripts. You can follow this link to fix this error: Powershell Running Scripts Is Disabled On This System Error.

Conclusion

In this blog post, we have seen different ways to connect to sharepoint online using powershell. You can utilize either Sharepoint online management shell or Sharepoint PnP cmdlets to complete your task. After you connect to sharepoint online using either methods you can execute sharepoint powershell scripts or use the Individual commands to perform any change.