How to Manage SharePoint via PowerShell - Part 1
Published Jul 28 2020 12:01 AM 16.4K Views
In this two-part series, we're going to look at how we can manage SharePoint using PowerShell. This is highly focused on SharePoint Online, but if the cmdlets are available, it also applies to SharePoint on-premises. We'll start with the basics, and then get some real-world scenarios scripts in part 2 to get you started with your daily management tasks. I'll also you give some tips along the way to make your life easier.

 

Let me start by asking this: Have you ever been curious enough to look at how many SharePoint on-premises PowerShell cmdlets are available? If not, I can tell you there are 800+ of them. Now, have you been curious enough to look at how many PowerShell cmdlets are available to manage SharePoint Online?
 
numOfCmdlets.png

 

 

Well, that's a big drop isn't it?! In SharePoint on-premises, there are a lot of commands to configure Search, Service Applications, interact with SQL databases, or other features provided by SharePoint.

With SharePoint Online, we don't need to do all that. Microsoft manages everything, and we don't need to worry about databases for instance.
 

So, what now?

There is another PowerShell module out there... And if you've never heard of it, you're totally missing out!

SharePoint PowerShell PnP (Patterns & Practices) is a module created by a community member (Erwin Van Hunen, MVP) to fill a gap in the management of SharePoint when using only PowerShell.

Currently, there are 492 cmdlets available (v.3.23.2007.1) and the prefix used is 'PnP' (i.e.: Get-PnPList).

If you don't have a native cmdlet, you can still use Client-Side Object Model (CSOM), or Server-Side Object Model (SSOM). However, as administrators, we might know PowerShell, but not be proficient in the other methods.

Note: In this module, you'll also notice that there are Microsoft Teams cmdlets, which are starting to surface with the July 2020 version!

 

How can I use it?

To start benefiting from this great module, we first need to install it. We have a few ways to do that.

  • Download the .msi
  • Install from the PowerShell Gallery

This module is available for SharePoint 2013/2016/2019, and SharePoint Online. It's important to refer to the official documentation because some commands might be available for a module but not the other.

Once installed with your favorite method, we need to connect to SharePoint Online (make sure you've got at least the SharePoint admin role assigned). Depending on the action we want to perform, we can connect to the SharePoint Admin Center, or to a particular site. At its most basic form, connecting would look like this:
$creds = Get-Credential

Connect-PnPOnline -Url "https//<TENANT-NAME>.sharepoint.com/sites/<YOUR-SITE>" -Credential $creds​
 
Spoiler
Want a tip? If you enter your credentials in your Windows Credential Manager under "Windows Credentials --> Add a generic credential", you won't need to use the -Credential parameter!
If your account has multi-factor authentication (MFA) enabled, use the -UseWebLogin parameter. I'd encourage you to visit the Connect-PnPOnline documentation because there are plenty of other ways to connect!
 

Connect to the SharePoint Admin Center

Depending on which site you're connected to, you get different results. What I mean is, by connecting to the SharePoint Admin Center you won't be able to interact much with library content, or fields. But connecting to the Admin Center can give us information about the tenant settings.

Let's connect to the Admin Center, and run Get-PnPTenant to get some information at the tenant level.
 
Connect-PnPOnline -Url "https://m365x141782-admin.sharepoint.com/"
Get-PnPTenant​
GetPnPTenant.png

 

As we are connected to the top-level site (admin one), we can also get all our site collections using the Get-PnPTenantSite cmdlet.
GetPnPTenantSite.png

 

Connect to a Specific SharePoint Site

This is where you're going to send most of your requests. Use the following to connect to your target site:
 
Connect-PnPOnline -Url "https://m365x141782.sharepoint.com/sites/Contoso"

Again here, I'm not using the -Credential parameter because my credentials are stored in the Credential Manager on my machine.

Spoiler
Want a Tip? If you ever forget on which site you're connected to, you can run `Get-PnPContext`, and look at the 'Url' property.

That's it for this first part. Part 2 will explore how to interact with sites content! Stay tuned.
 
 
Version history
Last update:
‎Jul 27 2020 07:30 AM
Updated by: