Managing SharePoint Online Using PowerShell

May 17, 2023
7 min read

The powerful collaboration tool SharePoint Online (SPO) is deeply integrated with Microsoft Teams and OneDrive. A SharePoint administrator needs to carefully automate, bulk manage, and configure these sites, users, and groups because it’s possible that tens of thousands of SharePoint sites each have thousands of users accessing them. Most of those users are managed using groups. To perform some of the more common SPO administrative tasks, you need a thorough understanding of how PowerShell integrates with SPO and how to use PowerShell to manage sites, users, and groups and how to generate PowerShell scripts.

PowerShell for SharePoint Online has several cmdlets for managing all aspects of SharePoint Online. The SPO PowerShell module offers three primary management activities:

  • Create SPO sites and configure the settings of these sites.
  • Manage SPO users and groups.
  • View SPO site group membership.

Note: You need to be a member of either the SharePoint Admins role or the global administrator role in Microsoft 365 in order to connect and manage SharePoint Online.

Installing SharePoint Online PowerShell 

Before you can begin using PowerShell SPO cmdlets, you have to install them. There are a couple of ways to complete this installation:

  • Install the SPO PowerShell module via PowerShell Gallery. 
  • Install the SPO PowerShell module manually by downloading and installing the .msi file.

This article explores the installation via PowerShell Gallery. In this PowerShell Window, you perform three actions including: checking to see if the SPO PowerShell module is already installed, installing the SPO PowerShell module, and updating the SPO PowerShell module to ensure that you have all the latest and greatest SPO cmdlets. 

To perform these tasks, use the following steps:

  1. Open Windows PowerShell in Administrator mode.
  2. Issue the Get-Module cmdlet specifying the SPO module name.
  3. If PowerShell for SPO is not installed, issue the Install-Module cmdlet to install it.
  4. Ensure that the latest cmdlets are available by issuing the Update-Module cmdlet specifying the SPO module.

Figure 1 shows these cmdlets being used to perform these actions.

A screen cap showing how to check for the SPO PowerShell module, install the module, and update the SPO PowerShell module using the cmdlets Get-Module, Install-Module, and Update-Module.
Figure 1: Installing and updating the PowerShell for SPO module View Full Size

Note: Alternatively, you can install the SharePoint Online Management Shell module by downloading and installing a .msi file that is located on this Microsoft site. 

Connecting to SharePoint Online Using PowerShell 

You next want to connect to and verify that you’re connected to SPO using PowerShell so you can begin managing and configuring SPO using PowerShell. Begin by using the Connect-SPOService cmdlet that pops up a window asking you to supply the credentials needed to complete the connection to SPO.

After a successful connection is established, issue the Get-SpoSite cmdlet to verify that the connection was successful and also display a list of site collections in SharePoint Online, as shown in Figure 2.

A screenshot showing the connection to SPO via PowerShell and verifying a successful connection using Get-SpoSite. It lists the name/URL, the owner, and the storage quota.
Figure 2: Connect to SPO using PowerShell, and retrieving SPO sites View Full Size

Important: When connecting to SPO, you need to specify the URL of your tenant name. This URL requires that you append -admin after your tenant name. Notice that when I connected to SPO in the Microtechpoint tenant in Figure 1, I used https://microtechpoint-admin.sharepoint.com. It’s the only service that requires -admin in the URL when you connect to it. 

Understanding SharePoint Online cmdlets

There’s a plethora of SPO PowerShell cmdlets, so I’m not going to discuss all of them. I’ll explore the most common cmdlets used to manage your SPO sites, users, and groups. The general cmdlets for managing these SPO components involve using five primary commands with a verb-noun pattern. There will be some examples following this overview.

  • Add-xxx: Adds a SharePoint component 
  • Get-xxx: Retrieves and displays information about a SPO component
  • New-xxx: Creates a new SPO component
  • Set-xxx: Modifies or changes settings on a SPO component
  • Remove-xxx: Deletes a SPO component

Common SharePoint Online cmdlets

The following are the cmdlets you’ll use most often.

Get-SPOSite, New-SPOSite, Set-SPOSite, Remove-SPOSite[MS1] 
Get-SPOUser, Add-SPOUser, Remove-SPOUser, Set-SPOUser
Get-SPOSiteGroup, New-SPOSiteGroup, Set-SPOSiteGroup, Remove-SPOSiteGroup
Get-SPOTheme, Add-SPOTheme, Remove-SPOThem

Obtaining Help with Available SharePoint Online PowerShell cmdlets

There are just too many SPO PowerShell cmdlets for anyone to remember all of them, but you can get help on all the SPO PowerShell cmdlets by using the following command:

Get-Command -Module Microsoft.Online.SharePoint.Powershell

Use the following command to display a list of SPO PowerShell cmdlets that begin with a verb, like Add:

Get-Command -Module Microsoft.Online.SharePoint.Powershell Add -*

Use the following command to display the details of a specific SPO PowerShell cmdlet:

Get-Help Add-SPOUser -Detail

Use the following command to see examples of a specific SPO PowerShell cmdlet:

Get-Help Add-SPOUser -Examples

Managing SPO Components in Bulk

If you have the need to create site collections or add users in bulk, you can create a .csv file containing the necessary fields, separated by commas, for the SPO object and then use SPO PowerShell to perform a bulk import. For instance, if you wanted to create several site collections, you can create a .csv file, called NewSiteCollections.csv, using a format similar to this:

Owner,StorageQuota,Url,Template,Name

In subsequent lines, provide the values for each site collection you want to create:

Brian@Microtechpoint.com,1000,
https://microtechpoint.sharepoint.com/sites/NewSiteName,STS#0,
”MTP Classic Site”

To create the new site collections, open SPO PowerShell in Administrator mode and execute the following command:

Import-Csv C:\users\Brian\Desktop\SiteCollections.csv | ForEach-Object
 {New-SPOSite -Owner $_.Owner -StorageQuota $_.StorageQuota -Url $_.Url
-NoWait -Template $_.Template -Title $_.Name}

To add users to the new site collections, create two additional .csv files. Call the first file GroupsPermissions.csv. It will store the groups and permissions by using a format similar to:

Site,Group,PermissionLevels

In subsequent lines, provide the values for each group and its permission level that you want to create:

https://microtechpoint.sharepoint.com/sites/NewSiteName,
”MTP Project Managers”,Edit

The second file, which you can call users.csv, stores the users you’re adding to the site collection using a format similar to:

Group,LoginName,Site

In subsequent lines, provide the values for each user that you want to add to the site collection:

“MTP Project Managers ”,Linda@Microtechpoint.com,
 https://microtechpoint.sharepoint.com/sites/NewSiteName

To add the groups to the new site collections, open SPO PowerShell in Administrator mode and execute the following command:

Import-Csv C:\users\brian\desktop\GroupsPermissions.csv |
ForEach-Object {New-SPOSiteGroup -Group $_.Group -PermissionLevels 
$_.PermissionLevels -Site $_.Site}

After the previous command completes successfully, add the users to the site collections by opening SPO PowerShell in Administrator mode and executing the following command:

Import-Csv C:\users\Brian\desktop\Users.csv | where {Add-SPOUser  
-Group $_.Group –LoginName $_.LoginName -Site $_.Site}

Alternatively, you can create a PowerShell script to execute the two previous commands from within the script file. Do so by creating a file called GroupsUsers.ps1, and copying and pasting the two Import-CSV commands into the file on separate lines, in the same order you ran them individually. 

You can execute the script file by opening SPO PowerShell in Administrator mode and executing the following command:

Set-ExecutionPolicy Bypass
C:\users\Brian\desktop\GroupsUsers.ps1

Using SharePoint Online PowerShell cmdlets

Figure 3 contains the commands used to create and manage a new SPO site by using the following steps:

  1. Using New-SPOSite, create a new SPO site called “MTP Sports” using the following parameters:
    1. Classic team site template, using template STS#0
    2. Assign site Title of “MTP Sports Info” 
    3. Assign StorageQuota of 1024 MBs
    4. Set Brian as the site collection Owner
  2. Run Get-SPOSite to display settings of the new site just created. 
  3. Use Set-SPOSite to modify the site StorageQuota by doubling the initial storage quota to 2048.
  4. Run Get-SPOSite to display new storage quota settings.
  5. Use Add_SPOUser to add Linda as a new user in the “MTP Sports Info Members” Group.
A screenshot showing the cmdlets used to create a new site, display it, modify the new site, display it again, and then add a user to the new site, as in the numbered list above the image.
Figure 3: Use PowerShell to create new SPO site, modify a site setting, and add a new user to the site. View Full Size

Figure 4 is a screen shot of the new SPO site showing the title of the site you added, and that Linda is a member of the default “MTP Sport Info Members” group located within the site. 

A screenshot of a new site created using the New-SpoSite PowerShell cmdlet. It’s called People and Groups > MTP Sports Info Members and has one member listed.
Figure 4: Newly created SPO Site interface View Full Size

As I mentioned previously, there are too many SharePoint Online PowerShell cmdlets to discuss here, but you can review and learn about all of them here: Microsoft.Online.SharePoint.PowerShell.

SharePoint Online and PowerShell could certainly change, so you should monitor this site for changes: Microsoft 365 Roadmap

Brian Alderman

Brian Alderman

Brian Alderman is a former Microsoft MVP, and has his Master's in Computer Information Systems. Since 1995, he has held several Microsoft certifications that currently include; MCT, MCSE for SharePoint, MCSA for Office 365, MCITP for SQL Server(R), MCSA and MCSE for Windows Server(R), and also his PMP certification. As a Senior Technical Instructor and Consultant, he has 30 years of experience in networking, SQL Server databases, SharePoint technologies, and project management. Brian's publications include Windows 2000 Professional, SQL Server 2000 Administration, SharePoint 2010 Administrator's Companion, and Microsoft SharePoint 2013 Administration Inside Out. He is an active speaker at SharePoint Industry conferences including SharePoint Saturday's, SPLive, DevIntersection, SharePoint Fest, Microsoft Ignite, and several international SharePoint conferences including London, Milan, and Madrid.