How to Connect to SharePoint Online from PowerShell?

I know I said it several times, but I’ll say it again: I’m a big fan of PowerShell! PowerShell is an administrative task automation and configuration management framework from Microsoft that allows you to administer Microsoft products, including SharePoint Online. PowerShell makes repetitive tasks easier and makes our life smarter, and it helps to automate complex tasks and reduces the risk of human errors. In this article, I’m exploring how to use PowerShell with SharePoint Online.

How to Connect to SharePoint Online using PowerShell?

Why should we use PowerShell to Manage SharePoint Online? If you’re a SharePoint administrator, you know that PowerShell is your best friend. With PowerShell, you can automate many tasks that would otherwise take a long time to complete. Not just that, There are several reasons why we use PowerShell:

  1. Perform bulk operations and speed up the process – E.g., Applying settings to all sites, document libraries, etc.
  2. Generate reports – E.g., Export all user permissions from a site
  3. Certain operations and configurations can only be done and available only through PowerShell
  4. Automate common repetitive tasks with the help of Windows Task Scheduler.
  5. Query and Filter the data
  6. Configure services, Manipulate objects, Monitoring, etc.

In this post, I am going to show you how to connect to SharePoint Online using PowerShell as a beginner. So, to start with, follow these steps to connect to SharePoint Online via PowerShell:

Step 1: Install the SharePoint Online Management Shell or SharePoint Online PowerShell Module

Make sure You have PowerShell and SharePoint Online PowerShell Module Installed. To download Windows Management Framework 3, which includes PowerShell 3.0: https://www.microsoft.com/en-us/download/details.aspx?id=34595

Download and Install SharePoint Online Management Shell
You have to download and install SharePoint Online Management Shell to start with. Download PowerShell for the SharePoint Online module at https://www.microsoft.com/en-us/download/details.aspx?id=35588

install powershell for sharepoint online
Update: Instead of downloading and installing the MSI file, you can also install the PowerShell module for SharePoint Online, if you are running with an Operating system like Windows 10, that supports PowerShell version 5 or newer: Install-Module -Name Microsoft.Online.SharePoint.PowerShell, Refer here for more info: Install SharePoint Online Management Shell Module via PowerShell

How to check the SharePoint Online Management Shell version? Use Control Panel >> Add/Remove programs to find out the installed version of SharePoint Online Management Shell! You can also use Control Panel – Add/Remove programs to uninstall the SharePoint Online management shell.

Step 2: Connect to SharePoint Online PowerShell using the Connect-SPOService cmdlet

Once the SharePoint Online Management Shell is installed, the next step is to connect to the SharePoint Online site using PowerShell. How do I access SharePoint Online Management Shell? Launch the “SharePoint Online Management Shell” command prompt from the start menu and connect to SharePoint Online Administration Center first and run the following command to connect to SharePoint Online: (Or you can use the Windows PowerShell console / PowerShell ISE as well to run a PowerShell script)

Connect-SPOService -Url https://salaudeen-admin.sharepoint.com `
           -credential [email protected]

This cmdlet must be executed before we use any other SharePoint Online cmdlets. As soon as you hit enter, you’ll get a login prompt to enter your username and password. Make sure you connect with an account with minimum permission of the SharePoint Online administrator and use HTTPS in the admin site URL. You’ll get a prompt for the password.

Connect to SharePoint Online with PowerShell

Step 3: Start using SharePoint Online PowerShell cmdlets!

Once connected, you can start managing SharePoint Online with PowerShell cmdlets for the tenant or for individual sites. Here are some examples of how to use the SharePoint Online management shell. Let’s create a site collection in SharePoint Online using PowerShell.

SharePoint Online PowerShell to Create Site Collection
Now you can access SharePoint Online from PowerShell and start managing SharePoint sites (From PowerShell console/SharePoint Online Management Shell or from PowerShell ISE). Let’s create a new site with the following cmdlet.

#Lets create a new Site collection:
New-SPOSite -Url https://salaudeen.sharepoint.com/sites/Sales `
   -Owner [email protected] -StorageQuota 1000 -Title "Sales Site" 
powershell script to connect to sharepoint online

Once you’ve finished, You can disconnect the session with “Disconnect-SPOService”. However, it’s not mandatory. Generally, it’s a good idea to leave the connection open until and unless you work with different tenants.

Do I need tenant Admin or Global Administrator rights to run PowerShell Cmdlets for SharePoint Online?
You need SharePoint Online administrator or Global Administrator rights in order to run these PowerShell scripts. However, to run CSOM or PnP PowerShell scripts at the site level, You just need Site collection Administrator rights.

Create a group in SharePoint Online with PowerShell ISE:
How to run PowerShell script for SharePoint Online? You can use PowerShell ISE to run the PowerShell script for SharePoint Online. Let’s create a group using PowerShell for SharePoint Online with the below commands.

#sharepoint online powershell create group:
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#connect to sharepoint online site collection using powershell
Connect-SPOService -Url https://salaudeen-admin.sharepoint.com `
      -credential [email protected]

#create group
New-SPOSiteGroup -Site https://salaudeen.sharepoint.com/sites/Sales  `
            -Group "Sales Managers" -PermissionLevels "Full Control" 
how to run powershell on sharepoint online

Even more generic code to connect to SharePoint Online through PowerShell would be:

#Import SharePoint Online PowerShell module
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

#SharePoint Admin Center URL - Set it accordingly
$AdminSiteURL= "https://crescent-admin.sharepoint.com"

#Get credentials to connect
$Credential = Get-Credential

#Connect to SharePoint Online services
Connect-SPOService -url $AdminSiteURL -Credential $credential

#Get Tenant settings
Get-SPOTenant

What if you don’t want to get the credentials prompt? E.g., Save credentials in the script and schedule them in the windows task scheduler. Here is how to connect to SharePoint Online from PowerShell using the user ID and password:

#Variables for processing
$AdminCenterURL = "https://crescent-admin.sharepoint.com"

#User Name Password to connect 
$AdminUserName = "[email protected]"
$AdminPassword = "Password goes here"

#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential $Credential
 
#Get all Site colections with SharePoint PowerShell
Get-SPOSite

Connect SharePoint Online PowerShell with MFA (Multifactor Authentication)

How do I connect to SharePoint Online with MFA enabled accounts from PowerShell? To connect with SharePoint Online where multifactor authentication is enabled, simply remove the -Credential parameter from the “Connect-SPOService” cmdlet.

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

Hit Enter, You’ll get a popup (which is MFA aware) to enter the credentials and the code from multifactor authentication. To learn more on connecting to SharePoint Online with MFA-enabled accounts, refer to my other post: PowerShell: Connect to SharePoint Online with MFA

PowerShell commands for SharePoint Online – Index of SharePoint Online PowerShell cmdlets:

For the list of available cmdlets for SharePoint Online, Refer: SharePoint Online PowerShell cmdlets at https://technet.microsoft.com/en-us/library/fp161364.aspx. You can also use this cmdlet to list SharePoint Online PowerShell cmdlets:

Get-Command -Module Microsoft.Online.SharePoint.PowerShell
connect to sharepoint online using powershell

This lists all Sharepoint Online PowerShell commands available. You may have noticed all the cmdlets will follow the pattern: <Verb>-SPO<Noun>. E.g., Add-SPOUser.

Limitation: Unlike SharePoint on-premises, SharePoint Online offers only the least set of PowerShell cmdlets to manage SharePoint Online. E.g., There are no direct PowerShell cmdlets to activate a feature, create a subsite, create a list, add a list item, get data from SharePoint list, download files from SharePoint, etc. The solution would be utilizing the client-side object model (CSOM) components with PowerShell! How to Connect to SharePoint Online using CSOM PowerShell?

Client Side Object Model (CSOM) is a subset of SharePoint Online Management Shell!

SharePoint Patterns and Practices (PnP) contains a library of PowerShell cmdlets that allows you to perform complex operations with one single cmdlet. To connect to SharePoint Online using PnP PowerShell, refer to: How to Connect to SharePoint Online using PnP PowerShell?

How do I create a new site collection in SharePoint Online using PowerShell?

To create a modern Team site collection in SharePoint Online, use New-SPOSite PowerShell cmdlet.
New-SPOSite -Url “Site-URL” -Owner “User-ID” -StorageQuota 2048 -Title “Site-Title” -Template “STS#3”
More info: PowerShell to Create a modern site collection in SharePoint Online

How do I run a PowerShell script in SharePoint Online?

You can run a PowerShell script by simply right-clicking on the .ps1 file and choosing “Run with PowerShell” from Windows File Explorer! You can also use PowerShell ISE to create, debug, and execute PowerShell scripts for SharePoint.
More info: How to Execute PowerShell Scripts in SharePoint?

How do I connect to SharePoint Online with MFA enabled accounts from PowerShell?

Just run the cmdlet “Connect-SPOService” without any “Credentials” parameter to it. This leads you to an MFA-enabled login popup for modern authentication. More info: Connect to SharePoint Online from PowerShell with MFA

How do I get items from a SharePoint Online list in PowerShell?

You can use CSOM or PnP PowerShell methods to get items from a SharePoint Online list. E.g., “Get-PnPListItem -List ‘List-Name'”
More info: Get a list of items from a SharePoint list in PowerShell

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

5 thoughts on “How to Connect to SharePoint Online from PowerShell?

  • How to get the list of existing sites and select the required.

    Reply
  • Is very helpful

    Reply
  • How to take backup and restore the site collection from dev to uat in sharepoint online

    Reply

Leave a Reply

Your email address will not be published.