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
- Go to Azure Portal and sign in.
- Navigate to Azure Active Directory > App registrations.
- Click New registration.
- Enter a name like
SharePointPnPApp. - Under Supported account types, choose Single tenant (unless multi-tenant access is needed).
- Click Register.
Step 2: Create a Client Secret
- In the registered app, go to Certificates & secrets.
- Click New client secret.
- Add a description and set an expiry duration.
- Click Add and copy the secret value. You won’t be able to see it again.
Step 3: Grant API Permissions
- Go to API permissions > Add a permission.
- Select SharePoint > Application permissions.
- Add the required permissions (e.g.,
Sites.Read.All,Sites.FullControl.All). - Click Add permissions.
- Click Grant admin consent for [Your Tenant] and confirm.
Step 4: Assign App Permissions in SharePoint
- Navigate to your SharePoint site (e.g.,
https://yourtenant.sharepoint.com/sites/yoursite). - Append
/_layouts/15/appinv.aspxto the URL. - Fill in the App ID (Client ID) and click Lookup.
- Add the following XML in the Permission Request XML box:
xmlCopyEdit<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl"/>
</AppPermissionRequests>
- 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.
