Updated PowerShell PnP connection script

image

One of the challenging things about manipulating SharePoint items with PowerShell was that you need to use PnP PowerShell. I have always found this tricky to get working and connect and it seems things have changed again.

Now when you want to use PnP PowerShell you can only do so using a Azure AD app! This is additionally challenging if you want to do that manually so I modified by free connection script:

https://github.com/directorcia/Office365/blob/master/o365-connect-pnp.ps1

to allow the creation of the Azure AD app as part of the process and to also allow you to specify an existing Azure Ad app that was already created by the process as the way to connect. The documentation is here:

https://github.com/directorcia/Office365/wiki/SharePoint-Online-PnP-Connection-Script

but in essence, you run the script the first time and it will create an Azure AD app for you like this:

image

Subsequent times you can simply use the apps again by specifying the ClientID GUID in the command line to make the connection. If you don’t then the script will create a new Azure AD app. So create an Azure AD app the first time and use that same app going forward I suggest. Of course, consult the full online documentation for all the details.

Hopefully, this makes it a little easier to use PnP PowerShell in your environment.

Connect to PnP PowerShell script

The latest pnp.powershell module (V2.X and above) now won’t work with PowerShell v5. Thus, I have updated my PnP connection script:

https://github.com/directorcia/Office365/blob/master/o365-connect-pnp.ps1

to accommodate this.

Thus, if you attempt to run this script in PowerShell version 5 with the latest pnp.powershell module you will typically see:

image

and the error is:

Could not load file or assembly ‘System.Management.Automation, Version=7.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.

However, when you run the script in PowerShell V7 you’ll see:

image

that the connection is successful.

I have also taken the opportunity to remove the dependency in the script as well on the now depreciated module MSONLINE and replaced it the Microsoft.Graph module.

This kind of signals the beginning of the end for modern cloud modules in PowerShell 5. However, some still require PowerShell 5 but I expect that to change.

In summary, the latest pnp.powershell modules require PowerShell version 7 and I have updated my connection script to accommodate this.