If you've spent any time administrating SharePoint, you've inevitably become frustrated with managing access, settings, and other items manually. Wouldn't it be great if there was a magic spell that would complete tasks quicker?

 

That magic spell comes in the form of PowerShell! SharePoint Online Management Shell (a PowerShell module) provides an easy way to complete bulk tasks, change settings quickly, and perform actions not otherwise available.

 

If you're new to PowerShell it may sound complicated and the image below might look like a foreign language. 

 

 

Don't worry—I'm giving a simple, step-by-step process you can follow to make your magic happen in SharePoint!

 

What Is It Exactly?

The SharePoint Online Management Shell is a Windows PowerShell module. Essentially, we'll be using a PowerShell module specifically for SharePoint. Check out this article from Microsoft to learn more about PowerShell.

 

How Does It Work?

PowerShell works through "cmdlets" or PowerShell commands. Cmdlets are lines of scripts written in a specific syntax that you can input into the module to change/add/update things on applications. 

 

The main difference between cmdlets in Windows PowerShell and the SharePoint Online Management Shell is the nouns used. Windows PowerShell starts with MSO, while SharePoint Online starts with SPO.

 

Simple cmdlets are easy to use even for beginners. Microsoft provides excellent documentation for many commands that users need to only tweak before running. 

 

In many cases, you'll mostly copy/paste with no need to learn the syntax or write scripts from scratch. I'll show you what I mean.

 

Prerequisite 

To use SharePoint Online PowerShell commands, you need the SharePoint Administrator or Global Administrator role in Microsoft 365. 

 

Installing the SharePoint Online Management Shell

Every Windows computer comes equipped with the Windows PowerShell app by default, but you'll need to install the SharePoint Online Management shell module.

 

Select the orange "Download" button on this page and install the application by opening the setup wizard and walking through the onscreen prompts.

 

Once the installation is complete, you're ready to run your first command!

 

Let's Get Running!

The first step is logging in to SharePoint Online via a simple cmdlet.


Use the sample cmdlet provided below. You'll need to replace the yellow highlighted section with the URL of your tenant's SharePoint Online admin center. 

 

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


Follow these steps to connect:

  1. Copy and paste "Connect-SPOService -Url" into a notepad or Word document. 
  2. Go to your SharePoint Admin Center and copy everything before the first "/". Paste that URL at the end of the command. 
  3. Copy and paste the whole command directly into the SharePoint Management Shell window (Use CTRL+V to make it easy). Press the "Enter" key to run the command. 

A pop-up will appear asking for your login information. Once you log in, you're connected! You'll need to repeat these steps each time you open the SharePoint Online Management Shell module.

 

Now What?

Now that you're connected let's get started doing some magic. 

Getting Information

Some cmdlets allow you to display the information you're looking for quickly. Others will get lists of sites, groups, and users.

 

1. Get a list of the sites in your tenant with this command:

Get-SPOSite

 

2. Get a list of the groups in your tenant with this command:

Get-SPOSite | ForEach {Get-SPOSiteGroup -Site $_.Url} | Format-Table

 

Note: This series of commands demonstrates a unique capability in PowerShell known as "piping." Each command is fed inputs from the previous command.

 

3. Get a list of the users in your tenant with this command:

Get-SPOSite | ForEach {Get-SPOUser -Site $_.Url}

 

4. Get all users and security groups for a specific SharePoint site.

Get-SPOUser –Site <Site URL>

 

Tip: Remember that to run a command, you must paste the cmdlet into the window and press the "Enter" key on your keyboard. 

 

Managing Users and Groups

There are a variety of cmdlets for streamlining your process of adding users and groups to a site. 

 

Adding a User to a Group

1. Use the Add-SPOUser cmdlet to add users to a group. 

Add-SPOUser -Site <Site URL> -LoginName <User Email> -Group <Group Name>


Adding a Large List of Groups

The easiest way to add a large list of groups to sites is to prepare a CSV file and then utilize a PowerShell command.


1. Create a CSV file with headers/columns corresponding to the parameters defined in the PowerShell script. In this case, we're adding groups to sites, so we'll need to define Group, Site, and Permission Levels. 


The CSV should have those three column headers with the corresponding data below. Make sure you format your data as a table with headers. See the sample below. 

 

 

2. Use the Import-Csv command as shown below, replacing the highlighted section with the file path for the CSV.

Import-Csv C:\Users\JohnSmith\Downloads\Groups.csv | ForEach {New-SPOSiteGroup -Group $_.Group -PermissionLevels $_.PermissionLevels -Site $_.Site}

 

Adding a Large List of Users

Similarly, you can add one or more users to one or more groups.


1. Create a CSV file with headers/columns corresponding to the parameters defined in the PowerShell script. In this case, we're adding users to groups, so we'll need to define Group, LoginName, and Site. 


(Note: There's no need for Permission Levels since the group will already have permissions applied).


The CSV should have those three column headers with the corresponding data below. Make sure you format your data as a table with headers.


2. Use the Import-Csv command as shown below, replacing the highlighted section with the file path for the CSV.

Import-Csv C:\Users\JohnSmith\Downloads\Users.csv | ForEach {Add-SPOUser -Group $_.Group –LoginName $_.LoginName -Site $_.Site}

 

Ready to Get Started?

This only brushes the surface of what can be done quickly through simple PowerShell cmdlets. Check out Microsoft's Cmdlet reference to get started with simple cmdlets for frequently performed tasks in SharePoint. 


Looking for additional tutorials? Read more from the C5 Insight blog!