Using the Patterns and Practices PowerShell with SharePoint

PowerShell Text Purple hero

If you are the type of person who needs to manage SharePoint with PowerShell, then do I have good news for you. If you find the PowerShell cmdlets that Microsoft provides for Online to be lacking, then I have even more good news for you.  By the way, everyone should fit into both of those categories. This good news is not about the recent updates Microsoft made to the SPO management shell. It is about a far superior tool called Patterns and Practices (PNP) PowerShell. And while the focus is on using PNP with SharePoint Online, there are versions for on-premises, too. Keep reading.

 

 

What Is Patterns and Practices

If you are not familiar with PNP, then most likely, you are not a developer. This is good because I write for admins, not those yucky devs. Anyway, the PNP team started in 2013 to help guide developers on how to transition from BPOS-D to BPOS. If you were not around back then, BPOS stood for Business Productivity Online Services. It was the first version of what we now know and love as Office 365. Of course, you are not here for a history lesson.

Here in 2017, the team is made up of a group of Microsoft consultants backed by community contributions. They develop guidance and functionality for things like the SharePoint Framework, Office 365 APIs, and Microsoft Graph. While that all might be cool, we do not really care. What we do care about is that they use all of those learnings to create their own PowerShell. That is amazing. It is not just for SharePoint Online. They also have versions for SharePoint 2013 and 2016. Everyone wins.

Installing the PNP PowerShell

The easiest way to get the PNP PowerShell is to install them from the gallery. The below example shows you how to install it for SharePoint Online.

  1. Open PowerShell by right-clicking on your PowerShell icon. Run it as an administrator. I recommend doing this on your local desktop computer.
  2. At the PowerShell prompt type Install-Module SharePointPnPPowerShellOnline.
  3. If you want to install the NuGet Provider, type Y. Press Enter.
  4. If you trust the PSGallery, type Y. Press Enter.

In approximately 30 seconds to one minute, the module will be downloaded and installed for you. And that is all folks. You can now do magic. If you have on-prem SharePoint, step two should be adjusted as shown below.

  • 2013: Install-Module SharePointPnPPowerShell2013
  • 2016: Install-Module SharePointPnPPowerShell2016

Is PowerShell Not Working for You?

If none of this works for you, it is because you are using an older version of PowerShell. It is probably on Windows 7 or 8. No worries. Watch the video Make PowerShell Easier to Use. It will walk you through updating your old, rusty PowerShell to the new hotness. Run through those steps quickly. Don’t worry, I will wait.

Still Not Able to Get It Working?

Your final option is to use an MSI Installer. You can download the latest release here. This gets you the same result. It just is not as cool as using PowerShell.

A Quick Security Change

PowerShell security varies based on what version of PowerShell you are running and on which operating system. You need to double check your settings. In your PowerShell window, type Get-ExecutionPolicy. If you get Restricted, then you will need to change that. If you get any other value, you should be good to go. If you need to make a change, I recommend that you use the Set-ExecutionPolicy RemoteSigned command. We are now talking about changing your PowerShell security settings. Therefore, I would also encourage you to better understand the settings. The good news is that RemoteSigned is the most restrictive setting that will allow you to use the PNP PowerShell.

Connecting to SPO with PNP

It would not be very nice of me to tell you about all of this and not at least get you started using it. For simplicity sake, we will just prompt for a username and password. If you want to see much better ways to manage your PowerShell passwords, and you do, then check out this article I wrote called Managing Usernames and Passwords with PowerShell for SharePoint Online. The Windows Credential Manager method is my favorite, especially with PNP. Again, I will wait while you catch up.

To connect to your SPO tenant, make sure you have PowerShell still open as an administrator:

  1. At the PowerShell prompt type $me = Get-Credential.
  2. A credential request box will open. Enter your username and password. Click OK. Remember, this is your SharePoint Online administrator account.
  3. At the PowerShell prompt type Connect-PNPOnline -url  https://<yourtenant>.sharepoint.com -credential $me. Replace <yourtenant> with the name of your actual tenant.

That does it. You are now connected to your PowerShell site collection. You could type Get-PNPWeb to see a list of all of your subsites. Or type Get-PnPTenantSite to see a list of all of the site collections in your tenant. Lots of fun.

From here, I will leave you to explore the fun that is PowerShell. It is great having this much power. If you want to see a list of all of the cmdlets, you can play with type Get-Command -Module SharePointPnPPowerShellOnline. As of April 2017, you should see 391 joyous options. Enjoy.

Last Two Things

To help you get all of this installed, there is also a companion video available called Install Patterns and Practices PowerShell for SharePoint Online. You will notice instead of Get-PNPWeb, I use Get-SPOWeb. This is because they transitioned from the SPO prefix to the PNP prefix. This was due to conflicts with the Microsoft native cmdlets. Not a big deal but it is something to note. The second thing to be on the look out for is more SPO articles using this wonderful PowerShell. You can view them right here on the Petri IT Knowledgebase. The next one in the hopper, which is why I wrote this install guide, shows you how to move files on SharePoint Online using PowerShell. We will talk about using this instead of the browser. Pretty exciting!

Until next time,

Shane

@ShanesCows