Microsoft 365 – Few approaches / options for Connecting to tenant using PnP PowerShell – Connect-PnPOnline – Part 1

Microsoft 365 - Connecting to Microsoft tenant using PnP PowerShell - Connect-PnPOnline
Microsoft 365 - Connecting to Microsoft tenant using PnP PowerShell - Connect-PnPOnline

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

Today discussion on one more very useful PowerShell CMDLET 🙂

Background : Many times I or my team need write few PowerShell scripts where we need to connect to Microsoft 365 tenant. Sometime we uses PnP PowerShell cmdlet.

So here in 2 part series we will discuss few approaches to connect to Microsoft 365 Tenant using PnP PowerShell cmdlet – Connect-PnPOnline and respective use cases – when to use which approach

Prerequisites :

  • If SharePoint PnP module is not installed, kindly please install by using following one of the option
  • Execute the following command

Install-Module SharePointPnPPowerShellOnline 

Details :

Different options available to connect Microsoft 365 tenant using Connect-PnPOnline are

  • Using Interactive parameter as
    • When we use –Interactive parameter, we get prompted for user name and password to login
    • Once we connected with valid user name and password we are ready to execute the PnP CMDLETs (context will be ready to execute other PnP commands)
    • We could use this option while
      • testing our PowerShell scripts OR
      • only one time execution OR
      • occasionally manually execution
Connect-PnPOnline -Interactive

Microsoft 365 - Connecting to Microsoft tenant using PnP PowerShell - Connect-PnPOnline
Fig : Microsoft 365 – Connecting to Microsoft tenant using PnP PowerShell – Connect-PnPOnline
Microsoft 365 - Connecting to Microsoft tenant using PnP PowerShell - Connect-PnPOnline, on successful connection - ready to execute the PnP cmdlets - Here executing Get-PnPConnection
Fig : Microsoft 365 – Connecting to Microsoft tenant using PnP PowerShell – Connect-PnPOnline, on successful connection – ready to execute the PnP cmdlets – Here executing Get-PnPConnection
  • Using Credentials (Get-Credential) parameter
    • When we use -Credential (Get-Credential) parameter, we will get prompted for user name and password
    • Once we entered credentials successfully, we are ready to execute other PnP CMDLETs

Connect-PnPOnline -Url https://knowledgejunction1.sharepoint.com -Credentials (Get-Credential)

Microsoft 365 - Connecting to Microsoft tenant using PnP PowerShell - Connect-PnPOnline - Using parameter -Credentials (Get-Credential)
Fig : Microsoft 365 – Connecting to Microsoft tenant using PnP PowerShell – Connect-PnPOnline – Using parameter -Credentials (Get-Credential) – prompt for user name and password

Fig : Microsoft 365 – Connecting to Microsoft tenant using PnP PowerShell – Connect-PnPOnline – Using parameter -Credentials (Get-Credential) – after successfully entering the credentials – executing other PnP cmdlet

Connect-PnPOnline -Url "https://knowledgejunction1.sharepoint.com" -ClientId <MY APP CLIENT ID> -ClientSecret <MY CLIENT SECRET>

Microsoft 365 - Connecting to Microsoft tenant using PnP PowerShell - Connect-PnPOnline - Using ClientId and ClientSecret
Fig : Microsoft 365 – Connecting to Microsoft tenant using PnP PowerShell – Connect-PnPOnline – Using ClientId and ClientSecret

$password = (ConvertTo-SecureString -AsPlainText 'myprivatekeypassword' -Force)

Connect-PnPOnline -Url "https://knowledgejunction1.sharepoint.com" -ClientId <MY CLIENT ID> -CertificatePath 'c:\mycertificate.pfx' -CertificatePassword $password  -Tenant 'knowledgejunction1.onmicrosoft.com'

In next article we will discuss some more approaches 🙂 STAY TUNED…

Thanks for reading 🙂 HAVE A FANTASTIC TIME AHEAD 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

  1. December 18, 2021

    […] last article – Microsoft 365 – Few approaches / options for Connecting to tenant using PnP PowerShell –… we discussed few approaches / options for connecting SharePoint site / generating context using PnP […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: