Smiling woman connecting to SharePoint Online

SharePointOnlinePowerShell: How to connect to SharePoint Online

Besides the possibility to manage your SharePoint Online in the SharePoint admin center, you have the option to do it with PowerShell. For this tasks Microsoft has published the PowerShell module SharePointOnlinePowerShell. It’s focus is more administrative tasks, than process automation. You can e.g. change the SharePoints Tenants policies, but you cannot create list items with this PowerShell module. The module is constantly developed by Microsoft it bears 223 cmdlets (16.08.2021).

Scope of SharePointOnlinePowerShell

I recommend checking the scope with following cmdlet:

Get-Command -Module Microsoft.Online.SharePoint.PowerShell | out-gridview -passthru

Prerequisites

  • If you want to connect to SharePoint Online with SharePointOnlinePowerShell, you need a user with the SharePoint Administrator role.
  • You need Windows PowerShell 2.0 or higher to run the module

Installation of SharePointOnlinePowerShell

The name for the installation is different then in the documentation. The technical name is Microsoft.Online.SharePoint.PowerShell. You can install the module Microsoft.Online.SharePoint.PowerShell with following PowerShell cmdlet:

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

If you have not trusted PSGallery yet, you will be prompted if you trust this repository. You can confirm it with “y”.

Screenshot of the installation of SharePointOnlinePowerShell

If you encounter this issue, start the PowerShell Session as an administrator:

PS C:\Users\Serkar> Install-Module -Name Microsoft.Online.SharePoint.PowerShell
Install-Module : Administrator rights are required to install modules in 'C:\Program Files\WindowsPowerShell\Modules'.
Log on to the computer with an account that has Administrator rights, and then try again, or install
'C:\Users\Serkar\Documents\WindowsPowerShell\Modules' by adding "-Scope CurrentUser" to your command. You can also try
running the Windows PowerShell session with elevated rights (Run as Administrator).
At line:1 char:1
+ Install-Module -Name Microsoft.Online.SharePoint.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Install-Module], ArgumentException
    + FullyQualifiedErrorId : InstallModuleNeedsCurrentUserScopeParameterForNonAdminUser,Install-Module

You can start PowerShell as an administrator like this:

Start-Process powershell -Verb runAs

You can also download it manually from PSGallery: PowerShell Gallery | Microsoft.Online.SharePoint.PowerShell 16.0.21513.12000

Connect to SharePoint Online with SharePointOnlinePowerShell

You can connect to SharePoint Online with an credential object or interactively. In this post I am describing both scenarios. Currently there is now way to do it with an Azure enterprise application, so you need a user. If you want to automate processes, I highly recommend a service user for this, since you don’t want your automation to crash, when you leave the company.

Connect to SharePoint Online with SharePointOnlinePowerShell with Credential

Note: You can only connect with an user with the SharePoint administrator role. It also won’t function, if you have multi factor authentication (MFA) enabled. For MFA check out the interactive instruction of this post.

In the first step create an credential object:

$Credential = Get-Credential
credential object for connecting to SharePoint Online

After doing this, replace the adminurl and connect to SharePoint Online:

Connect-SPOService -Url "ADMINURL" -Credential $Credential

In my case it looks like this:

Connect-SPOService -Url "https://devmodernworkplace-admin.sharepoint.com/" -Credential $Credential

If you got no error, you have established the connection successfully.

Connect to SharePoint Online with SharePointOnlinePowerShell interactively

You can connect interactively with following cmdlet:

Connect-SPOService -Url "ADMINNURL"

In my case it is:

Connect-SPOService -Url "https://devmodernworkplace-admin.sharepoint.com/"

You will see, that a login prompt will pop up:

Login prompt, when logging in interactively

Disconnect from SharePoint Online

Disconnecting from SharePoint Online can be done like this. If you close your PowerShell Sesssion, you don’t have to do it. The connection is dropping automatically.

Disconnect-SPOService

Conclusio

As you can see, there are at least two PowerShell modules – PNP.PowerShell and SharePointOnlinePowerShell to manage and automate your SharePoint tenant. With this module you can approach your Sharepoint Tenant more on as an administrator, than as somebody, who wants to automate business processes. I would not miss this module out of sight, when automating processes, because some cmdlets might be missing in the PNP PowerShell module or they might not work as expected.

Further Reading

If you want to focus more on automating processes, than on administering SharePoint, you should definitely check out how to connect with PNP.Powershell: Connect to SharePoint with PowerShell | SharePoint Online (sposcripts.com)

If you want to see the original docs of Microsoft check out this article: Erste Schritte mit der SharePoint Online-Verwaltungsshell. | Microsoft Docs

This Post Has 3 Comments

Leave a Reply