How to Use Connect-PnPOnline in Powershell

How to Use Connect-PnPOnline in Powershell

Maximizing the potential of SharePoint Online is easier by running PnP PowerShell cmdlets.

PnP PowerShell presents itself as an open-source solution backed up by a very active community, which allows for more control over SharePoint site design.

However, before unleashing the power of these cmdlets, we first need to connect to PnP Online. In this guide, we'll show you how to use Connect-PnPOnline in PowerShell by following five easy steps.

What is Connect-pnponline?

Connect-PnPOnline is a PowerShell cmdlet in the SharePoint PnP PowerShell module. As mentioned before, SharePoint Patterns and Practices (PnP) is a community-driven initiative that provides a set of guidelines, practices, and tools to help developers and administrators work with SharePoint more efficiently.

SharePoint Online is a cloud-based service created by Microsoft that provides organizations with a secure and scalable platform for collaboration, document management, and information sharing.

That being said, PnP PowerShell cmdlets are used to manage the creation, retrieval, updating, and removal of site designs and site scripts within both new and established modern site collections.

So, in order to access all of these features, it is necessary to use Connect-PnPOnline first, as it serves as the bridge that connects us with the vast array of SharePoint Online capabilities.

Connect-PnPOnline Command Syntax & Meaning

The syntax of Connect-PnPOnline is the following:

Connect-PnPOnline -Url https://tenant-admin.sharepoint.com -Interactive

In this case, each parameter and switch on this syntax has a specific meaning:

  • Connect-PnPOnline: This is the main PowerShell cmdlet from the SharePoint PnP PowerShell module we are talking about, used to establish a connection to a SharePoint online site;
  • Url: The -Url parameter is used to specify the URL of the SharePoint Online site to which you want to connect. In this case, the URL is "https://tenant-admin.sharepoint.com," which needs to be modified to match the URL that grants permissions for PnP to work.
  • Interactive: The -Interactive parameter is used to prompt the user for credentials interactively. When this parameter is included, a pop-up window will appear, allowing the user to enter their username and password.

What Can You Use the Connect-PnPOnline Cmdlet for?

The Connect-PnPOnline cmdlet in the SharePoint PnP PowerShell module is used to establish a connection to a SharePoint Online site, enabling you to perform various administrative and development tasks using PowerShell.

Therefore, Connect-PnPOnline will only provide you with the connection you need to start using other cmdlets in the SharePoint PnP PowerShell module.

Afterward, you'll have access to a wide variety of benefits and features, such as the following:

  • Manage Site Collections: You can use PnP PowerShell cmdlets to create, retrieve, and delete site collections. For example, you can create a new site collection using the New-PnPTenantSite cmdlet;
  • Work with Lists and Libraries: PnP PowerShell allows you to manage lists and libraries. You can create, modify, and delete lists, as well as add, update, and remove items from lists;
  • Handle Permissions: With PnP PowerShell, you can manage permissions at various levels, including site, list, and item levels. You can grant or revoke permissions for users and groups;
  • Automate Site Configuration: PnP PowerShell allows you to automate site configuration tasks. For instance, you can apply a custom theme, change site settings, or configure features;
  • Content Migration: You can use PnP PowerShell to migrate content between sites or site collections. This includes copying or moving files and folders.

5 Prerequisites to Use Connect-PnPOnline in PowerShell

Before being able to use Connect-PnPOnline, we first need to comply with a series of prerequisites:

  1. SharePoint PnP PowerShell Module Installed: First and foremost, it is necessary to have the SharePoint PnP PowerShell module installed in our system. Otherwise, the cmdlet will not work as intended. Below, we'll teach you how to install this module easily;
  2. SharePoint Online Tenant URL: Since we are attempting to connect to a SharePoint Online environment, we need the URL of the Online Tenant. We'll need to add this URL before running the Connect-PnPOnline command;
  3. User Account with SharePoint Online Permissions: In order to comply with the authentication flow, it's necessary to have a user account that contains the collection of permission requests for the SharePoint Online client;
  4. SharePoint Administrator Rights: To run some PnP PowerShell cmdlets, it will be necessary to have SharePoint Administrator rights;
  5. Supported and Updated PowerShell Version: Make sure you are using a version of PowerShell that is compatible with the SharePoint PnP PowerShell module. You can check the latest PowerShell version on the Microsoft Learnwebsite.

How to Use Connect-PnPOnline

Now that you are aware of the importance of the Connect-PnPOnline cmdlet, it's time to learn how to use it.

Follow these five quick steps to use Connect-PnPOnline in PowerShell.

Step 1: Connect to Exchange Online PowerShell

First, we need to connect to Windows PowerShell as an Administrator. To do so, launch PowerShell on your computer, and on the command window, run the following cmdlet:

Connect-ExchangeOnline -UserPrincipalName [email protected]

Replace "[email protected]" with your email address, use your Microsoft Account credentials, and complete the login.

After successfully logging into your account, you can proceed with the following step.

Step 2: Install PnP PowerShell for SharePoint Online

Now, in order to run the desired command, we first need to install PnP PowerShell for SharePoint Online.

To do so, open the window, and run the following command:

Install-Module PnP.PowerShell

Running this cmdlet downloads and installs the module you need to get started.

Step 3: Run Get-Module PnP.PowerShell to Confirm Installation

Sometimes, the installation process can be hindered by multiple factors.

To prevent this from happening, we recommend you run the following command after performing installation:

Get-Module "PnP.PowerShell" -ListAvailable

The Get-Module "PnP.PowerShell" command confirms the installation by displaying the PnP PowerShell module information. The information returned on the screen will let you know about the status of the module installation.

Step 4: Use Connect-PnPOnline

Once you have installed the right module in its updated version, it's time to run Connect-PnPOnline.

Open the command window, and type in the following cmdlet:

Connect-PnPOnline -Url https://tenant-admin.sharepoint.com -Interactive

In this case, it is necessary to replace the URL with your Tenant Admin URL, as shown in the following example:

Connect-PnPOnline -Url https://meetingroom365.sharepoint.com -Interactive

You'll be asked to enter your credentials to connect. Go back to the Prerequisites Part to check out the requirements to run the cmdlet before proceeding.

Step 5: Start Using PnP PowerShell Cmdlets

Once you have successfully connected to PnPOnline, you'll be ready to use PnP PowerShell cmdlets.

Some of the most common PnP PowerShell cmdlets include the following:

  • Set-PnPSiteScriptPackage
  • Set-PnPSiteScript
  • Set-PnPSiteDesign
  • Revoke-PnPSiteDesignRights
  • Remove-PnPSiteScript
  • Remove-PnPSiteDesignTask
  • Remove-PnPSiteDesign
  • Invoke-PnPSiteDesign
  • Grant-PnPSiteDesignRights
  • Get-PnPSiteScriptFromWeb
  • Get-PnPSiteScriptFromList
  • Get-PnPSiteScript
  • Get-PnPSiteDesignTask
  • Get-PnPSiteDesignRunStatus
  • Get-PnPSiteDesignRun
  • Get-PnPSiteDesignRights
  • Get-PnPSiteDesign
  • Add-PnPSiteScriptPackage
  • Add-PnPSiteScript
  • Add-PnPSiteDesignTask
  • Add-PnPSiteDesign

Keep in mind that Connect-PnPOnline just works to connect you with the SharePoint Online environment as requested - to make further changes in SharePoint Cloud Services, it'll be necessary to use other cmdlets.

Why is Connect-PnPOnline Not Working & How to Fix It

Did you try to connect to PnPOnline using the specific cmdlet, but failed to do so? In this case, there are three reasons behind this error.

1. Outdated Version of PnP PowerShell Module

One of the most common reasons why you can't run Connect-PnPOnline is that you are using an outdated version of the PnP PowerShell module.

To fix this, go to the command window, and run the following cmdlet:

Update-Module SharePointPnPPowerShellOnline

This will update the existing module, allowing you to run the command as requested.

2. Incorrect Microsoft Credentials

While attempting to connect to PnP Online, make sure to type in the correct information in the credential prompt.

If you fail to add the right info into the credential popups, then the server will be unable to authenticate your login,resulting in authentication errors.

In case you need additional permissions, contact your organization administrators for further help.

3. MFA (Multi-Factor Authentication) is Enabled

If Multi-Factor Authentication is enabled for the account, the standard username/password authentication may not work directly.

In this scenario, you may need to use an app password or an alternative authentication method.

Consider using the -UseWebLogin parameter with Connect-PnPOnline for an interactive login, or generate an app password for non-interactive scenarios.

So, instead of adding -Interactive at the end of the command syntax, run the Connect-PnPOnline cmdlet as shown below:

Connect-PnPOnline -Url https://yourtenant.sharepoint.com -UseWebLogin

This results in an alternative, secure, and straightforward method to authenticate with SharePoint Online, preventing potential authentication errors.

Summary: How to Use Connect-PnPOnline in PowerShell

The Connect-PnPOnline command is the window to a world of opportunities - it connects us with several PnP PowerShell cmdlets that can highly improve our productivity and performance.

These are the three most important points to consider about the Connect-PnPOnline cmdlet:

  1. Connect-PnPOnline PowerShell is used to connect to the SharePoint Online Admin Center of your tenant, allowing you more customization features and control;
  2. Before running Connect-PnPOnline, make sure that you have the necessary credentials and updated module version installed on your device;
  3. After running Connect-PnPOnline, and successfully verifying the module installation, you are ready to start running several PnP PowerShell cmdlets to your advantage.