Getting started with PnP PowerShell - Installation and app registration

3 min read 20 days ago
Published on Sep 13, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the installation of PnP PowerShell and the registration of an app in your tenant Entra ID. PnP PowerShell is a powerful tool for managing SharePoint and Microsoft 365 environments. This guide covers two authentication methods: app-only and interactive, enabling you to automate tasks or run commands directly in the console.

Step 1: Install PnP PowerShell Module

To begin using PnP PowerShell, you need to install the module. Follow these steps:

  1. Open PowerShell as Administrator

    • Right-click the PowerShell icon and select "Run as administrator."
  2. Install the Module

    • Enter the following command to install the PnP PowerShell module:
      Install-Module -Name PnP.PowerShell -RequiredVersion 2.10.0
      
    • If prompted, confirm the installation.
  3. Verify Installation

    • To ensure that the module is installed correctly, run:
      Get-Module -ListAvailable | Where-Object { $_.Name -eq 'PnP.PowerShell' }
      

Step 2: Register an App in Entra ID

App registration is necessary for granting permissions to perform operations using PnP PowerShell. Follow these steps:

  1. Access Azure Portal

    • Navigate to the Azure Portal and sign in with your administrator account.
  2. Create a New App Registration

    • Go to "Azure Active Directory" and select "App registrations."
    • Click on "New registration."
    • Fill in the required fields:
      • Name: Enter a name for your app.
      • Supported account types: Choose the appropriate option (e.g., Accounts in this organizational directory only).
    • Click "Register."
  3. Configure API Permissions

    • Select your newly created app and go to "API permissions."
    • Click on "Add a permission" and choose "Microsoft Graph."
    • Select "Application permissions" and add the necessary permissions (e.g., Sites.Read.All, Sites.ReadWrite.All).
    • Click "Grant admin consent" for the permissions.
  4. Generate Client Secret

    • Go to "Certificates & secrets."
    • Click "New client secret" and provide a description and expiration period.
    • Copy the generated secret value and store it securely.

Step 3: Authenticate Using App-Only Method

This method is useful for automated tasks such as scheduled operations. Here’s how to authenticate:

  1. Use the Connect-PnPOnline Command

    • In PowerShell, enter the following command with your tenant details:
      $tenantId = "YourTenantId"
      $clientId = "YourClientId"
      $clientSecret = "YourClientSecret"
      Connect-PnPOnline -Tenant $tenantId -ClientId $clientId -ClientSecret $clientSecret -AppOnly
      
  2. Verify Connection

    • Run a simple command to ensure the connection is successful:
      Get-PnPWeb
      

Step 4: Authenticate Using Interactive Method

For tasks performed directly in the PowerShell console, you can use interactive authentication:

  1. Connect Using Interactive Login

    • Enter the following command in PowerShell:
      Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -UseWebLogin
      
    • A login prompt will appear; enter your credentials.
  2. Check the Connection

    • Again, validate the connection with:
      Get-PnPWeb
      

Conclusion

You have successfully installed PnP PowerShell and registered an app in your Entra ID. You can now authenticate using either app-only or interactive methods, enabling you to manage your SharePoint and Microsoft 365 environments effectively.

Next Steps

  • Explore additional PnP PowerShell commands to automate tasks.
  • Consider checking out the PnP PowerShell documentation for more advanced functionalities.