SharePoint Online (SPO) is a powerful collaboration tool that is deeply integrated with Microsoft Teams and OneDrive. With the possibility of having tens of thousands of SharePoint sites with thousands of users accessing these sites, most of which will be managed by using groups, it is important for a SharePoint administrator to have the ability to automate, and bulk manage and configure these sites, users, and groups. With that being said, we need to have a thorough understanding of how PowerShell integrates with SharePoint Online and how you can use PowerShell to manage sites, users, and groups as well as generate PowerShell scripts to perform some of the more common SPO administrative tasks.

PowerShell for SharePoint Online has several cmdlets for managing all aspects of SharePoint Online, and the SPO PowerShell module offers three primary management features:

  • Create SPO sites and configuring setting of these sites
  • Manage SPO users and groups
  • View SPO site group membership

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

Installing SharePoint Online PowerShell

Before you can begin using these PowerShell SPO cmdlets you have to install them and 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

We are going to explore the installation via PowerShell Gallery. In this PowerShell Window we will perform three actions including; checking to see if the SPO PowerShell module is already installed, installing the SPO PowerShell module, and finally updating the SPO PowerShell module to ensure we have all the latest and greatest SPO cmdlets.

We will use the following steps to perform these tasks:

  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 latest cmdlets are available by issuing the Update-Module cmdlet specifying SPO module

This window shows these cmdlets being used to perform these actions.

Note: Alternatively we 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

We next want to connect to and verify we are connected to SPO using PowerShell. We will begin by using the Connect-SPOService cmdlet which will popup a window asking for our credentials needed to complete the connection.

After a successful connection is established we will issue the Get-SpoSite cmdlet to verify our connection was successful and also display a list of site collections in SharePoint Online as shown in the following screen.

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 when I connected to SPO in the Microtechpoint tenant I used https://microtechpoint-admin.sharepoint.com. It is the only service that requires -admin in the url when we connect to it.

Understanding SharePoint Online cmdlets

There is a plethora of SPO PowerShell cmdlets, so we are not going to discuss all of them, but we will explore the most common cmdlets used to manage your SPO sites, users, and groups. The general cmdlets for managing these SPO components involves using five primary commands that use a verb-noun pattern with some examples following this overview.

  • Add-xxx Adds a SharePoint component
  • Get-xxx Retrieve and display information about a SPO component
  • New-xxx Create a new SPO component
  • Set-xxx Modify or change settings on a SPO component
  • Remove-xxx Delete a SPO component

Common SharePoint Online cmdlets

  • Get-SPOSite, New-SPOSite, Set-SPOSite, Remove-SPOSite
  • Get-SPOUser, Add-SPOUser, Remove-SPOUser, Set-SPOUser
  • Get-SPOSiteGroup, New-SPOSiteGroup, Set-SPOSiteGroup, Remove-SPOSiteGroup
  • Get-SPOTheme, Add-SPOTheme, Remove-SPOTheme

Obtaining help on 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 a comma, for the SPO object and use SPO PowerShell to perform a bulk import. For instance, if you wanted to create several site collections, you can create a .csv file, we can call NewSiteCollections.csv, using a format similar to below;

Line 1: Owner,StorageQuota,Url,Template,Name

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

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

To create the new site collections, you’ll 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 you’ll create two additional .csv files. The first file, we can call GroupsPermissions.csv, which will store the groups and permissions by using a format similar to below;

Line 1: Site,Group,PermissionLevels

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

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

The second file, which we can call users.csv, will store the users you are adding to the site collection using a format similar to below;

Line 1: Group,LoginName,Site

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

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

To add the groups to the new site collections, you’ll 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, you execute the following command to 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. We do so by creating a file, which we can call GroupsUsers.ps1, and copying and pasting the two Import-CSV commands into the file on separate lines, in the same order we 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

The following PowerShell screen contains the commands used to create and manage a new SPO site using the following steps.

  1. Create site called “MTP Sports” using this information
    • Classic team site template
    • Site title of “MTP Sports Info”
    • Storage quota of 1024 MBs
    • Brian as the site collection owner
  2. After creating the site, we will display the settings of the new site we just created
  3. Modify the site by doubling the initial storage quota to 2048 MB’s
  4. After modifying the site, we will display the new storage quota settings
  5. Add Linda as a new user to the site in the “MTP Sports Info Members” group

This screen of our new SPO site shows the title of the site we added, and that Linda is a member of the default “MTP Sport Info Members” group located within this site.

As I mentioned previously there are too many SharePoint Online PowerShell cmdlets to discuss but you can review and learn about all of these cmdlets by accessing this Microsoft.Online.SharePoint.PowerShell web site.

Furthermore, SharePoint Online and PowerShell could certain change, so you should monitor these possible changes by following this site Microsoft 365 Roadmap.