PnP Powershell (and how to download a file from SharePoint)

 Concerning SharePoint, there is an exorbitant amount of differing information out there. Much of it is outdated. Furthermore it may refer to SharePoint 2010, 2013, 2019 or SharePoint Online.

Many smart people try to research how to do simple things in SharePoint and end up feeling stupid because the instructions don't work for a hundred different reasons.

Let me help out the smart people and let you know that you're not alone.

As of the beginning of 2021, if you want to connect to SharePoint Online you will want to start here... https://github.com/pnp

PnP stands for "Patterns and Practices". You can use these to connect to various M365 resources. For example to import or export data. Microsoft used to maintain PnP and it was available for download from them directly. Now PnP is open source and maintained by MVP (Microsoft Valued Professionals) on GitHub. You have a valid concern if you asked yourself "Why is this on GitHub and not Microsoft.com?". It's because Microsoft wants the community to maintain it. It is legit and going pretty well at this point having recently come out of Beta.

SharePoint is part of M365. So if you want to write a script to connect to SharePoint and do something you will use PnP. Most likely the PnP for PowerShell here... https://github.com/pnp/powershell

There are numerous older ways of connecting to SharePoint. You can use Visual Studio. You might also see things for CSOM or CAML or some web services or something else completely. I am just going to be talking about PowerShell right now so we can keep things straight.

First off, you need the PnP module. As long as you don't have an older version of PnP it should install with one line of PowerShell script. If you have a different version search here for how to fix that. (https://github.com/pnp/powershell/discussions)

Install it one time by running this...

Install-Module -Name "PnP.PowerShell"


You will get a prompt to confirm and off you go.

In order to connect to SharePoint now, you have choices. For me I went to AzureAD and registered an Application. This will work for now but if you have MFA (MultiFactor Authentication) you will have extra steps to research.

Here's a link on how to get your ApplicationID/ClientID. https://blog.velingeorgiev.com/how-register-sharepoint-app-new-azure-ad-portal

With this ClientID in hand (Or ApplicationID if you will) you are ready to run your script. Here it is.

$URL = "https://YOURSHAREPOINTSITE.sharepoint.com/Sites/SITENAME"
$login = "Username@YOURSHAREPOINTSITE.com";
$pwd = "Password";


$pwd = ConvertTo-SecureString $pwd -AsPlainText -Force;
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $login,$pwd;


Connect-PnPOnline -Url $URL -Credentials $Credentials -ClientId ApplicationIDFromAzureAD


Get-PnPFile "/DocumentLibraryName/Filename.xlsx" - Path "C:\TempOrSomethingThatExists" -FileName Filename.xlsx -AsFile -Force

You need to change the URL, Login, Password and filenames. You want to make sure the $URL is set to the site where you want to copy the file from.

You will notice it takes the password and converts it to a secure string. This is a mild way of keeping your password safe(ish). The beef is the last few lines. You connect with Connect-PnPOnline. You download the file with Get-PnPFile.

That's it. It seems simple but there are so many moving parts that you may hit your head. For example your company may require your login to use a certificate instead of a ClientId. Or perhaps you did not grant the right permissions to your Application in AzureAD.

If you have issues go to the above mentioned GitHub discussion. The people that write the module actually respond pretty quickly and exceptionally nicely to honest requests for help.

Comments