PowerShell code

PNP.PowerShell: How to connect to SharePoint Online with PowerShell

This article describes how to connect to SharePoint Online by using the PowerShell Module PNP.PowerShell


  • An global admin approval for the enterprise app PNP.PowerShell
  • Internet access

Installation PNP.PowerShell

I recommend using the latest PNP.PowerShell Module to connect to SharePoint Online. PNP stands for Patterns and Practices

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 '' 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 -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
[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

Connect to SharePoint Online

Connect interactively to SharePoint Online

To connect to SharePoint Online interactively, type following cmdlet:

Connect-PnPOnline -Interactive -Url "<yoursiteurl"

In my demo case it is

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

You just have to enter the credentials of an user with global admin privileger for the first run, afterwards, you can make use of your user or an serviceuser.

Note: You have to make use of a user with global admin permissions for the first time you connect to SharePoint Online. The global admin is needed to grant the admin consent for the enterprise application.

Login prompt when interactively connecting to SharePoint Online

As you can see, you were prompted admin consent for the enterprise application. This is only needed for the first run of the Connect-PNPonline script, since the enterprise application is registered after your consent.

Permissions requested for SharePoint Online

You don’t have to check the checkbox, to connect.

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


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


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


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 “PNP.PowerShell: How to connect to SharePoint Online with PowerShell”

  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