PowerShell code

How to Connect to SharePoint Online with PnP PowerShell (2025)

This article describes how to connect to Connect to SharePoint Online with PnP PowerShell. PNP stands for Patterns and Practices. You would like to use PNP.Powershell to create, read, update or delete items like SharePoint Sites, Lists, Libraries, Items.

Prerequisites

  • To setup and grant the App registration, you will need global admin approval for the enterprise app PNP.PowerShell

Installation PNP.PowerShell

I recommend using the latest PNP.PowerShell Module to connect to SharePoint Online.

You can find it here:

PnP PowerShell | PnP PowerShell

If your proxy don’t restrict the traffic, you can try to make use of following cmdlet

Install-Module -Name PnP.PowerShell
Installation of  Modules in PowerShell

If you get an error like me, start PowerShell as an administrator

Install-Module : Administrator rights are required to install modules in 'C:\Program Files\WindowsPowerShell\Modules'.
Log on to the computer with an account that has Administrator rights, and then try again, or install
'C:\Users\Serkar\Documents\WindowsPowerShell\Modules' by adding "-Scope CurrentUser" to your command. You can also try
running the Windows PowerShell session with elevated rights (Run as Administrator).
At line:1 char:1
+ Install-Module -Name PNP.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Install-Module], ArgumentException
    + FullyQualifiedErrorId : InstallModuleNeedsCurrentUserScopeParameterForNonAdminUser,Install-Module

You can do it straight from PowerShell through this cmdlet:

Start-Process powershell -Verb runAs

If you get this prompt, you can confirm both with “y”.

NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
 provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\Serkar\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by
running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install    and import the NuGet provider now?                                                                                      [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y                                                                                                                                                                                      Untrusted repository                                                                                                    You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y

You can now see all PNP cmdlets with this cmdlet:

Get-Command -Module PNP.Powershell

Setup App Registration

In order to Connect to SharePoint Online with PnP PowerShell, you need to setup an app registration in Entra ID. Depending whether, you want to run the scripts interactively or in the background (App Only access), different permissions are required.

InteractiveApp only access (Background)
Permission TypeDelegated PermissionsApplication Permissions
Authentication TypeUser Context (MFA supported)Certificate Based
PurposeAdmins want to run code interactively e.g. in a console.

Note: This approach will not work for Azure Automation or Azure Function, as you cannot interact with the console as part of the authentication
Scripts need to run on a scheduled / event triggered basis e.g. on a task scheduler or as part of an azure automation runbook / azure function
Required Privileges to create the appGlobal AdministratorGlobal Administrator

Connect to SharePoint Online

Connect interactively to SharePoint Online

You know that you have connected succesfully, when the cmdlet
Get-PNPList shows you the lists of your site

Testing the connectivity to SharePoint Online

Connect with credential object to SharePoint Online

Note: This won’t work, if you have MFA enabled.

To connect to SharePoint online interactively, type following cmdlet:

$Credential = Get-Credential
Connect-PnPOnline -Url "<yoursiteurl" -Credential $Credential

In my demo case it is

$Credential = Get-Credential

Connect-PnPOnline -Url "https://m365x388358.sharepoint.com/sites/askhr" -Credential $Credential

Enter your credentials ( It don’t have to be an global admin user, if you have already granted the admin consent)

Credential prompt when connecting with an credential object

Disconnect Session

If you want to disconnect, make use of the cmdlet

Disconnect-PnPOnline

You can see, that you have been disconnected succesfully by running the cmdlet.

Get-PnPConnection

You should get following message:

PS C:\Users\Serkar> Get-PnPConnection
Get-PnPConnection : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline
commands which uses the -Url argument to connect.
At line:1 char:1
+ Get-PnPConnection
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-PnPConnection], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Base.GetPnPConnection

Troubleshooting

Error: underlying connection was closed

Connect-PnPOnline : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Connect-PnPOnline $SiteURL -Credentials $Creds

If you got this error, you have to change the security protocol settings to TLS 1.2. You just have to run the following cmdlet before connecting to SharePoint

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;

Further Documentation

If you are interested in how to deal with existing SharePoint connections in PowerShell, check out this post: Dealing with existing SharePoint connections | SPO Scripts

21 thoughts on “How to Connect to SharePoint Online with PnP PowerShell (2025)”

  1. Pingback: SharePoint Downloader | Comfortable file download in 2 steps

  2. Pingback: Upload documents to SharePoint with PowerShell solution

  3. Pingback: How To Download a SharePoint Library Using Graph API (PowerShell)

  4. Pingback: How to download files from SharePoint using Graph API (PowerShell)

  5. Pingback: How to Upload Files to SharePoint using Graph API PowerShell

  6. Pingback: Create SharePoint list items using Graph API (PowerShell)

  7. Pingback: How to get SharePoint List Items with Graph API (PowerShell) | SPO Scripts

  8. Pingback: SharePoint Online: How to get all lists and libraries

  9. Pingback: Get all sites and subsites in SharePoint: 2 PowerShell scripts

  10. Pingback: SharePoint recycle bin: How to restore files with PowerShell | SPO Scripts

  11. Pingback: 3 of the most Important SharePoint PowerShell Modules and Snappins

  12. Pingback: SharePoint Site collection Logo: How to Change it in 3 comfortable steps

  13. Pingback: SharePointOnlinePowerShell: How to connect to SharePoint Online

  14. Pingback: Add items to SharePoint Online lists with Powershell | SPO Scripts

  15. Pingback: How I add new Webparts to SharePoint Pages by PowerShell | SharePoint

  16. Pingback: Dealing with existing SharePoint connections - SPO Scripts

  17. Pingback: Use credentials in PowerShell - SPO Scripts

  18. Pingback: Deleting SharePoint Sites FAST - Yes really FAST! - SPO Scripts

  19. Pingback: Dealing with existing SharePoint connections - SPO Scripts

  20. Pingback: Filtering for SharePoint Items with CAML Queries – SharePoint Scripts

  21. Pingback: Getting FieldValues of Items – SharePoint Scripts

Leave a Comment