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

Prerequisites

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

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

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

This Post Has 13 Comments

Leave a Reply