How to Connect to SharePoint Online using PowerShell

This guide will show you how to connect to SharePoint online using the Connect-SPOService cmdlet using either Web Logon or by using a PowerShell Credential object.

Connect Using Web Logon

Connecting using Web Logon is by far the simplest way to connect to SharePoint Online PowerShell, however, as you will see it is not the best option if you plan on connect often or in a script.

To connect, there is one simple command that you need to run, as follows:

Connect-SPOService -URL https://yourtenant-admin.sharepoint.com

When you run this command, you will then be prompted by the familiar Microsoft Account Login Screen in a popup window.

Microsoft Web Logon Box

Enter your credentials as normal and you will have successfully connected to SharePoint Online using PowerShell. You can easily verify that you have an active connection by running the following command:

Get-SPOSite -Limit 1

This command will fetch just one of your Site Collections to validate the connection.

Connect Using PowerShell Credentials

If you are planning on connecting to SharePoint Online regularly or as part of a script, you will not want to use Web Logon as you will need to re-enter your credentials every time you need to connect. Instead PowerShell offers you a secure method of storing credentials in an Object.

To do this, run the following command:

$credentials = Get-Credential

You will then be presented with the following PowerShell Login Box:

PowerShell Logon Box

Enter your Credentials and Click OK.

Your credentials are now stored in a PowerShell Credential Object called “$credentials”. This can be used to connect to SharePoint Online by providing it to the Connect-SPOService cmtlet as follows.

Connect-SPOService -URL https://yourtenant-admin.sharepoint.com -Credential $credentials

When you submit the command you will notice you are no longer prompted to provide login credentials to authenticate, instead it is using the credentials that are being stored in the PowerShell Object. That object will remain valid and usable until the PowerShell window is closed.

Copyright © 2020 - Martin Day