-
Notifications
You must be signed in to change notification settings - Fork 247
SharePoint Online PnP Connection Script
The o365-connect-pnp.ps1 script provides a robust, automated solution for connecting to SharePoint Online using PnP PowerShell with modern authentication requirements. It includes advanced Microsoft Graph compatibility fixes, automatic Azure AD app registration, and comprehensive error handling.
- Microsoft Graph v2.26 Compatibility Fix: Automatic detection and resolution of assembly loading issues
- Modern PnP Authentication: Compliant with September 2024 PnP PowerShell requirements
- Automatic Azure AD App Registration: Creates custom apps for interactive login
-
Existing App Support: Use pre-configured Azure AD apps with
-ClientIdparameter - Comprehensive Module Management: Automatic installation and updates of required modules
- PowerShell 7+ Compatibility: Enhanced for modern PowerShell environments
- PowerShell 7.0 or above (Required)
- Internet connection for module installation/updates
- Azure AD permissions to create app registrations (for automatic app creation)
- (Optional) Existing Azure AD app with SharePoint permissions
| Parameter | Type | Default | Description |
|---|---|---|---|
-prompt |
Switch | $false |
Set to $true to prompt for user input during installations |
-noupdate |
Switch | $false |
Set to $true to skip module updates (faster execution) |
-debug |
Switch | $false |
Set to $true to create detailed log file |
-ClientId |
String | $null |
Specify existing Azure AD App Client ID for PnP connection |
.\o365-connect-pnp.ps1.\o365-connect-pnp.ps1 -ClientId "12345678-1234-1234-1234-123456789012".\o365-connect-pnp.ps1 -prompt $true -debug $true.\o365-connect-pnp.ps1 -noupdate -ClientId "your-app-id".\o365-connect-pnp.ps1 -debug $true -noupdate $true- PowerShell Version Check: Validates PowerShell 7+ requirement
- Parameter Display: Shows current configuration and ClientId status
- Logging Setup: Creates transcript file if debug mode enabled
The script performs comprehensive version compatibility checks:
- Checks current
Microsoft.Graphmodule version - Identifies problematic versions (especially v2.26.0)
- Detects version mismatches between sub-modules
- v2.26.0 Detection: Automatically uninstalls and downgrades to stable v2.25.0
- Version Mismatch Fix: Removes all Graph modules and reinstalls with synchronized versions
- Compatibility Verification: Tests assembly loading and module imports
- ✅ v2.25.0: Stable and recommended
⚠️ v2.26.0: Problematic - automatically fixed- ✅ v2.28.0+: Compatible newer versions
- ❌ < v2.25.0: Upgraded to stable version
- Microsoft.Graph (v2.25.0 or compatible)
- Microsoft.Graph.Identity.DirectoryManagement
- Microsoft.Graph.Sites
- PnP.PowerShell
- Compares local vs. gallery versions
- Prompts for updates (if
-promptenabled) - Automatic updates (if
-promptdisabled)
- Connects with scopes:
Sites.Read.All,sites.ReadWrite.All,Domain.Read.All - Establishes authentication context for tenant operations
- Retrieves tenant domain information
- Auto-detects SharePoint tenant URL
- Retrieves all available SharePoint sites
- Displays interactive site selection grid
- Returns selected site URL for PnP connection
# Uses provided Client ID
connect-pnponline -url $siteUrl -Interactive -ClientId $ClientIdTroubleshooting for Existing Apps:
- Verifies Client ID format
- Checks SharePoint permissions
- Validates redirect URI configuration
- Confirms public client settings
# Creates new app automatically
$appRegistration = Register-PnPEntraIDAppForInteractiveLogin -ApplicationName $appName -Tenant $tenantDomain
connect-pnponline -url $siteUrl -Interactive -ClientId $newClientIdApp Creation Process:
- Generates unique app name:
CIAOPS-PnP-PowerShell-YYYYMMDD-HHMMSS - Registers with tenant domain
- Configures appropriate permissions
- Returns Client ID for future use
- Direct interactive connection (legacy support)
- Manual app registration guidance
- Comprehensive error messaging
Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider'
Solution: Automatically detected and fixed by version compatibility check
For Existing Apps:
- Verify Client ID is correct
- Check app has SharePoint permissions
- Ensure redirect URI includes
http://localhost - Confirm app is configured as public client
For New Apps:
- Ensure Microsoft Graph connection is active
- Verify tenant admin permissions
- Check network connectivity
This script requires PowerShell version 7 or above
Solution: Upgrade to PowerShell 7+ from https://github.com/PowerShell/PowerShell
If automatic app registration fails, follow these steps:
Register-PnPEntraIDAppForInteractiveLogin -ApplicationName 'CIAOPS-PnP-App' -Tenant yourdomain.onmicrosoft.com- Navigate to Azure Portal > Entra ID > App Registrations
- Click New Registration
-
Name:
CIAOPS PnP PowerShell -
Redirect URI:
http://localhost(Public client/native) - Copy the Application (client) ID
- Re-run script with:
.\o365-connect-pnp.ps1 -ClientId <your-client-id>
When -debug $true is specified:
- Creates transcript file:
../o365-connect-pnp.txt - Logs all script operations
- Captures error details
- Includes module version information
Parent directory of script location
Example: If script is in C:\Scripts\, log is in C:\o365-connect-pnp.txt
- Microsoft Graph: Sites.Read.All, sites.ReadWrite.All, Domain.Read.All
- Azure AD: Application registration permissions (for automatic app creation)
- SharePoint: Site access permissions
- Apps are created with minimal required permissions
- Public client configuration for interactive authentication
- Unique naming prevents conflicts
- Client IDs can be reused for consistent access
.\o365-connect-pnp.ps1 -noupdate -ClientId "existing-app-id"- Skips module update checks
- Uses existing app (no creation overhead)
- Reduces execution time significantly
- Modules are installed once per system
- Version checks prevent unnecessary updates
- Import operations are optimized
After first run, the script displays:
💡 TIP: Save this Client ID for future use with -ClientId parameter
Future usage: .\o365-connect-pnp.ps1 -ClientId 12345678-1234-1234-1234-123456789012
You can set a default Client ID in your PowerShell profile:
$env:DEFAULT_PNP_CLIENT_ID = "your-client-id"For unattended execution:
# Use existing app, no prompts, no updates
.\o365-connect-pnp.ps1 -ClientId $clientId -noupdate- ✅ Microsoft Graph v2.26 compatibility fix
- ✅ Modern PnP authentication support
- ✅ Automatic Azure AD app registration
- ✅ Existing app support via
-ClientIdparameter - ✅ Enhanced error handling and troubleshooting
- September 2024: PnP PowerShell requires custom app registration
- PowerShell 7+: Required for modern module compatibility
- Create Centralized Apps: Register organization-wide Azure AD apps
- Distribute Client IDs: Share approved Client IDs with users
- Monitor App Usage: Track app registrations and usage
- Security Reviews: Regularly audit app permissions
-
Use Existing Apps: Leverage
-ClientIdparameter for consistency -
Enable Debugging: Use
-debugfor troubleshooting - Version Control: Include Client IDs in secure configuration
- Error Handling: Implement retry logic for automation
- Save Client IDs: Note Client IDs for future use
-
Update Regularly: Run without
-noupdateperiodically -
Use Fast Mode: Employ
-noupdatewith existing apps for speed - Report Issues: Use debug logs for support requests
Script provided as is. Use at own risk. No guarantees or warranty provided.
Last updated: August 2025