Source: https://o365info.com/connect-pnp-powershell/


Must use Powershell version 7 for this to work


To connect to SharePoint Online with PowerShell, you must first Install the PnP PowerShell module. Then, create an application registration in Microsoft Entra ID. After that, you can use the Connect-PnPOnline cmdlet to connect interactively without entering your credentials. In this article, you will learn how to connect to SharePoint Online using the PnP PowerShell module.

Create application registration in Microsoft Entra ID

To connect interactively to your tenant with PnP PowerShell, you must create an application registration in Microsoft Entra ID. There are two methods to do this:

  • PowerShell (automatically)
  • Microsoft Entra ID (manually)

Register PnP application with PowerShell (automatic)

To register the PnP application with PowerShell, you must run the command below. You must specify the tenant domain name to register the application in Microsoft Entra.

Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP Rocks" -Tenant "ms365info.onmicrosoft.com" -Interactive

A prompt will appear where you must sign in with your global admin credentials. After that, another prompt will appear to accept the permissions.

How to Connect to SharePoint Online using PnP PowerShell login prompt

The application client ID will appear at the end of the PowerShell output. Ensure that you copy and save it because you need to use it to connect with PnP later.

dedcb709-a56b-4a92-93b1-23114a219e9f

Register PnP application in Microsoft Entra ID (manual)

There are a few steps involved when registering an application in Microsoft Entra ID. First, you need to register an application. Then, you need to add an authentication platform. Lastly, you need to assign API permissions to the PnP application.

Step 1. Register new application

Register an application in the Microsoft Entra ID with the steps below:

  1. Sign in to Microsoft Entra ID
  2. Click Identity > Applications > App registrations
  3. Click New registration
How to Connect SharePoint Online using PnP PowerShell register new application in Microsoft Entra
  1. Type the Name PnP SharePoint
  2. Select Accounts in this organization directory only (Single tenant)
  3. Click Register
How to Connect SharePoint Online using PnP PowerShell register application
  1. You successfully created an application, and the Application (client) ID appears
How to Connect SharePoint Online using PnP PowerShell with application client ID

Step 2. Add authentication to application

To add an authentication to the application, follow these steps:

  1. Click Authentication on the application overview page
  2. Click Add a platform
Connect SharePoint Online using PnP PowerShell add authentication
  1. Click Mobile and desktop applications
Configure platform in Microsoft Entra
  1. Type custom redirect URls http://localhost (this should really be http and not https)
  2. Click Configure
Configure custom redirect URLs localhost

Step 3. Assign API permissions to application

To assign API permissions to the application, follow these steps:

  1. Click API permissions on the application overview page
  2. Click Add a permission
Assign API permissions to the application PnP SharePoint in Microsoft Entra
  1. Click Microsoft APIs
  2. Click SharePoint
Request Microsoft API permissions SharePoint
  1. Click Delegated permissions
  2. Type AllSites
  3. Expand AllSites
  4. Select AllSites.Read
  5. Click Add permissions
Reguest delegated permissions in Microsoft Entra
  1. Click Grant admin consent for m365info
  2. Click Yes
Grant admin consent confirmation in Microsoft Entra ID
  1. The green check mark shows that you granted admin consent successfully
Granted admin consent successfully in Microsoft Entra ID

Connect PnP PowerShell

To connect to PnP PowerShell, you need to specify your SharePoint URL and application Client ID.

Connect-PnPOnline -Url "ms365info-admin.sharepoint.com" -Interactive -ClientId "dedcb709-a56b-4a92-93b1-23114a219e9f"

To check that you are connected to SharePoint Online, run the Get-PnpSite cmdlet below.

Get-PnPSite

Disconnect PnP PowerShell

Always disconnect your PnP PowerShell session when you finish your work.

Disconnect-PnPOnline

That’s it!