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 makes repetitive tasks easier and makes our life smarter. 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?
Follow these steps to connect to SharePoint Online via PowerShell.

Step 1: Install 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:

To Download and Install SharePoint Online PowerShell module:
You must download and install SharePoint Online Management Shell to start with. Download PowerShell for SharePoint online at
install powershell for sharepoint online
Instead of downloading and installing the MSI file, you can also install the PowerShell module for SharePoint Online using: Install-Module -Name Microsoft.Online.SharePoint.PowerShell

How to check SharePoint Online Management Shell version? Use Control Panel >> Add/Remove programs to find out the installed version of SharePoint Online Management Shell!

Step 2: Connect to SharePoint Online with PowerShell
Here is how to connect to the SharePoint Online site using PowerShell. Launch "SharePoint Online Management Shell" from the start menu and connect to SharePoint Online Administration Center first and run this PowerShell script to connect to SharePoint Online:

Connect-SPOService -Url `
           -credential [email protected]
This cmdlet must be run before we use any other SharePoint Online cmdlets. Make sure you connect with an account with minimum permission of 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. Here are some examples. 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
#Lets create a new Site collection:
New-SPOSite -Url `
   -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.

Create a group in SharePoint Online with PowerShell ISE:
How to run PowerShell script for SharePoint Online? You can use PowerShell ISE to run PowerShell script for SharePoint Online. Let's create a group using PowerShell for SharePoint Online.
#sharepoint online powershell create group:
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Connect to SharePoint Online
Connect-SPOService -Url `
      -credential [email protected]

#create group
New-SPOSiteGroup -Site  `
            -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

#Admin Center URL of your SharePoint Online - Set it accordingly
$AdminSiteURL= ""

#Get credentials to connect
$Credential = Get-Credential

#Connect to SharePoint Online services
Connect-SPOService -url $AdminSiteURL -Credential $credential
What if you don't want to get the credentials prompt? E.g. Save credentials in the script and schedule it in the windows task scheduler. Here is how to connect to SharePoint Online from PowerShell using user id and password:
#Variables for processing
$AdminCenterURL = ""

#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

Connect SharePoint Online PowerShell with MFA (Multifactor Authentication)
To connect with SharePoint Online where multifactor authentication is enabled, simply remove the -Credential parameter from "Connect-SPOService" cmdlet.
Connect-SPOService -Url
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 my other post: PowerShell: Connect to SharePoint Online with MFA

Index of SharePoint online PowerShell cmdlets:
For the list of available cmdlets for SharePoint online, Refer: SharePoint online 2013 PowerShell cmdlets at
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

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 list item, etc. The solution would be utilizing client side object model (CSOM) 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 SharePoint Online using PnP PowerShell, refer: How to Connect to SharePoint Online using PnP PowerShell?


  1. How to take backup and restore the site collection from dev to uat in sharepoint online

    1. You need 3rd party tools like ShareGate to copy-paste sites as of today!


Please Login and comment to get your questions answered!

Powered by Blogger.