Connect to SharePoint Online using PowerShell

For automation, integration, or administrative tasks, using PowerShell to access SharePoint Online with an Azure AD registered app is a secure and scalable approach. This method avoids user-based authentication and leverages app-only permissions.

In this guide, we’ll walk through the entire process—from registering an Azure AD app to connecting to SharePoint Online using PowerShell.


Prerequisites

  • Access to Microsoft Azure Portal
  • Admin access to the Microsoft 365 tenant
  • PowerShell installed with the PnP PowerShell module

Step 1: Register the App in Azure AD

  1. Go to Azure Portal and sign in.
  2. Navigate to Azure Active Directory > App registrations.
  3. Click New registration.
  4. Enter a name like SharePointPnPApp.
  5. Under Supported account types, choose Single tenant (unless multi-tenant access is needed).
  6. Click Register.

Step 2: Create a Client Secret

  1. In the registered app, go to Certificates & secrets.
  2. Click New client secret.
  3. Add a description and set an expiry duration.
  4. Click Add and copy the secret value. You won’t be able to see it again.

Step 3: Grant API Permissions

  1. Go to API permissions > Add a permission.
  2. Select SharePoint > Application permissions.
  3. Add the required permissions (e.g., Sites.Read.All, Sites.FullControl.All).
  4. Click Add permissions.
  5. Click Grant admin consent for [Your Tenant] and confirm.

Step 4: Assign App Permissions in SharePoint

  1. Navigate to your SharePoint site (e.g., https://yourtenant.sharepoint.com/sites/yoursite).
  2. Append /_layouts/15/appinv.aspx to the URL.
  3. Fill in the App ID (Client ID) and click Lookup.
  4. Add the following XML in the Permission Request XML box:
xmlCopyEdit<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/>
</AppPermissionRequests>
  1. Click Create and Trust It when prompted.

Step 5: Install PnP PowerShell Module

Open PowerShell as Administrator and run:

Install-Module PnP.PowerShell -Scope CurrentUser

Step 6: Connect to SharePoint Online Using the App

Use the following script in PowerShell:

$tenant = "yourtenant"
$siteUrl = "https://$tenant.sharepoint.com/sites/yoursite"
$clientId = "your-app-client-id"
$clientSecret = "your-app-client-secret"

Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret -Tenant "$tenant.onmicrosoft.com"

Step 7: Test the Connection

Once connected, test by running:

Get-PnPWeb

You should see details of the SharePoint site, confirming the app-only authentication is working.


Conclusion

Using an Azure AD registered app to connect to SharePoint Online via PowerShell is a secure and automated method, ideal for backend scripts and integrations. With app-only authentication, you avoid password management and user interaction, making it a best practice for enterprise-level automation.

Leave a Reply

Your email address will not be published. Required fields are marked *