Are you tired of manual operations and want to automate SharePoint tasks? Look no further! With the PnP PowerShell, you can connect to SharePoint and automate various operations quickly and easily. Let’s explore how to connect to SharePoint Online using PnP PowerShell, a powerful tool that simplifies SharePoint administration tasks.
SharePoint Patterns and Practices (PnP) PowerShell is a set of cmdlets designed to automate and simplify common SharePoint tasks. It is an open-source powered by the Client-Side Object Model (CSOM) code. By taking advantage of its built-in cmdlets and simplified scripting implementations, PnP PowerShell enables us to decrease the number of lines in our scripts.
Main Difference Between Connect-PnPOnline and Connect-SPOService
- Simply put, ‘Connect-PnPOnline’ is for connecting to a specific SharePoint site, while ‘Connect-SPOService’ is for managing tenant-level operations.
- While the SharePoint Online PowerShell module offers limited functionality for SharePoint Online administrators, such as managing sites and tenant settings, it may not be sufficient for more complex tasks.
- PnP PowerShell Cmdlets works in the context of the current user, whereas SPO command runs with the Tenant Admin rights.
- For more extensive functionality, it is recommended to utilize the SharePoint PnP PowerShell cmdlets project, which provides a wider range of cmdlets for SharePoint administration, development, and automation tasks.
To download and install the PnP PowerShell module, do the following cmdlet in Windows PowerShell as administrator. It is advisable to upgrade to PowerShell 7.x if you currently have PowerShell 5.x.
Firstly, uninstall the Legacy SharePointPnPPowerShellOnline Module if installed already.
Uninstall-Module SharePointPnPPowerShellOnline -Force –AllVersions
Now, you can install the new PnP.PowerShell module by running the below command.
Install-Module -Name PnP.PowerShell
It takes only a few seconds to download and install the PnP.PowerShell module once you confirm the prompt. If you try to connect to PnPOnline without installing the PnP.PowerShell module, then you will encounter the following error. “The term ‘Connect-PnPOnline’ is not recognized as the name of a cmdlet.”
Once the module is installed successfully, we need to run the below command to register Azure AD Application and Grant Access to the Office 365 tenant like below
By giving consent, then you can log into SharePoint Online.
Once you have installed the appropriate PnP PowerShell module, you can connect to SharePoint Online by running the following command.
Connect-PnPOnline -Url https://tenant-admin.sharepoint.com/
The cmdlet ‘Connect-PnPOnline’ prompts you to enter your credentials, and it establishes a connection to the SharePoint Online environment.
If you have enabled multi-factor authentication (MFA) on your Office 365 account, you need to use a slightly different approach to connect to SharePoint Online using PnP PowerShell.
Connect-PnPOnline -Url "https://contoso.sharepoint.com" -UseWebLogin
When you run the “Connect-PnPOnline” cmdlet with the “-UseWebLogin” switch, it will prompt you to log in using a web browser window.
Currently, there are 650 available in the PnP PowerShell module for SharePoint Online. That’s what makes PnP PowerShell such an effective tool for SharePoint administration and development tasks!
Here are some SharePoint operations you can perform using PnP PowerShell:
|Get-PnPSite||Lists all sites in SharePoint Online.|
|Add-PnPFile||Uploads a file to the web.|
|Add-PnPFolder||A folder will be created within a parent folder.|
|Add-PnPGroupMember||Adds a user to a group in Share Online.|
|Clear-PnPRecycleBinItem||Permanently deletes a specific recycle bin item.|
|Remove-PnPList||Removes a specific list in a SharePoint site.|
|New-PnPGroup||Adds a group to the Site Groups List.|
|Get-PnPHubSite||Lists all the hub sites in SharePoint Online.|
|Get-PnPSiteCollectionAdmin||Get the list of site collection administrators.|
|Remove-PnPUser||Removes a specific user from the site collection user information list.|
|Rename-PnPFolder||Renames a folder in SharePoint Online.|
|Save-PnPSiteTemplate||Saves a PnP site template to the file.|
|Update-PnPApp||Updates an app in SharePoint Online’s app catalog.|
|Unregister-PnPHubSite||Unregisters a site as a hub site.|
Execute the below cmdlet to update the existing PnP PowerShell module.
The “Update-Module” command is used to update a PowerShell module to the latest version available from an online repository.
To disconnect PnP PowerShell from the SharePoint Online environment, run the following cmdlet.
To uninstall the existing PnP PowerShell module, run the following cmdlet.
- PnP PowerShell provides a unified approach for developing solutions that work on SharePoint Online (cloud) environment.
- It is easy to install and configure in a development environment.
- PnP PowerShell provides a wide range of powerful commands that allow developers to perform complex operations in a single line of code.
- Since it offers a rich set of commands that saves development time and effort, developers can quickly accomplish their tasks.
Despite the advantages, PnP Powershell has a limitation to consider.
- PnP PowerShell is a community-driven project, and while it is actively maintained and updated, some commands may not work as expected or may not be available for certain scenarios.
I hope this blog helped with connecting to SharePoint Online using the PnP module. If you are working with multiple Office 365 workloads such as Azure AD, Exchange Online, Microsoft Teams, etc., connecting to Microsoft Graph will get your jobs done more easily.