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.
Table of contents
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.
A prompt will appear where you must sign in with your global admin credentials. After that, another prompt will appear to accept the permissions.

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.
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:
- Sign in to Microsoft Entra ID
- Click Identity > Applications > App registrations
- Click New registration

- Type the Name PnP SharePoint
- Select Accounts in this organization directory only (Single tenant)
- Click Register

- You successfully created an application, and the Application (client) ID appears

Step 2. Add authentication to application
To add an authentication to the application, follow these steps:
- Click Authentication on the application overview page
- Click Add a platform

- Click Mobile and desktop applications

- Type custom redirect URls http://localhost (this should really be http and not https)
- Click Configure

Step 3. Assign API permissions to application
To assign API permissions to the application, follow these steps:
- Click API permissions on the application overview page
- Click Add a permission

- Click Microsoft APIs
- Click SharePoint

- Click Delegated permissions
- Type AllSites
- Expand AllSites
- Select AllSites.Read
- Click Add permissions

- Click Grant admin consent for m365info
- Click Yes

- The green check mark shows that you granted admin consent successfully

Connect PnP PowerShell
To connect to PnP PowerShell, you need to specify your SharePoint URL and application Client ID.
To check that you are connected to SharePoint Online, run the Get-PnpSite cmdlet below.
Disconnect PnP PowerShell
Always disconnect your PnP PowerShell session when you finish your work.
That’s it!