0% found this document useful (0 votes)
18 views4 pages

PowerShell Cmdlets for SharePoint PnP

This tutorial provides a comprehensive guide on using PowerShell for SharePoint Online, focusing on the PnP.PowerShell module for automation tasks related to site, list, library management, and SPFx deployment. It includes setup instructions, command examples, user and permission management, file operations, and integration with Power Automate. The document also features practical exercises and best practices for effective usage of PowerShell in SharePoint environments.

Uploaded by

tharaniyadevi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

PowerShell Cmdlets for SharePoint PnP

This tutorial provides a comprehensive guide on using PowerShell for SharePoint Online, focusing on the PnP.PowerShell module for automation tasks related to site, list, library management, and SPFx deployment. It includes setup instructions, command examples, user and permission management, file operations, and integration with Power Automate. The document also features practical exercises and best practices for effective usage of PowerShell in SharePoint environments.

Uploaded by

tharaniyadevi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PowerShell for SharePoint (PnP & Admin Automation)

— Tutorial
Generated: 2025-10-15 07:30 UTC

Introduction
This guide covers PowerShell usage for SharePoint Online with emphasis on the [Link] module,
tenant/site automation, site/list/library management, permissions, SPFx deployment tasks, integration tips
with Power Platform, and real hands-on exercises you can practice. It is targeted at SharePoint developers
and administrators who want an end-to-end practical reference.

Table of Contents
1. Setup & Authentication 2. [Link] Basics (Cmdlets) 3. Site, List & Library Automation 4. User,
Group & Permission Management 5. File Operations & Data Import/Export 6. SPFx Deployment via
PowerShell 7. Integration: PowerShell + Power Automate / Power Apps 8. Advanced Provisioning &
Templates (PnP Provisioning) 9. Monitoring, Reporting & Best Practices 10. Exercises (Beginner →
Advanced) 11. Quick Cheat Sheet & Useful Links
1. Setup & Authentication
Install the [Link] module (cross-platform) — recommended over legacy SPO module for modern
tasks. Commands:
Install module (run as admin):
Install-Module -Name [Link] -Force

Connect interactive (MFA):


Connect-PnPOnline -Url [Link] -Interactive

Connect using AppId/Certificate (app-only):


Connect-PnPOnline -Url [Link] -ClientId -Tenant -Thumbprint

2. [Link] Basics (Cmdlets)


Common commands you'll frequently use. Run Get-Help -Detailed to explore parameters.
Get-PnPSite — Get site collection information
Get-PnPWeb — Get current web/site
Get-PnPList — List lists in site
New-PnPList — Create a new list
Add-PnPListItem — Add an item to a list
Get-PnPListItem — Retrieve list items
Add-PnPFile — Upload a file to a document library
Set-PnPListItem — Update a list item
Remove-PnPFile — Delete a file
Get-PnPGroup — Get SharePoint groups
Add-PnPGroupMember — Add user to group
Set-PnPListPermission — Set list permissions
Add-PnPApp — Upload & publish SPFx package to App Catalog

3. Site, List & Library Automation


Create modern communication or team sites, provision lists and libraries using scripts. Examples below.
Create a modern team site:
New-PnPSite -Type TeamSite -Title "Project Team" -Alias projectteam -Owners "owner@[Link]"

Create a custom list with fields:


New-PnPList -Title 'Projects' -Template GenericList -Url 'Lists/Projects' -OnQuickLaunch
Add fields:
Add-PnPField -List 'Projects' -DisplayName 'Status' -InternalName 'Status' -Type Choice
-AddToDefaultView -Choices @('New','In Progress','Completed')

4. User, Group & Permission Management


Manage users, create groups, and assign permissions. Use groups rather than individuals for scale.
Create a SharePoint group:
New-PnPGroup -Title 'Project Members' -AllowMembersEditMembership $false
Add user to group:
Add-PnPGroupMember -LoginName 'user@[Link]' -Identity 'Project Members'
Grant permissions to a list:
Add-PnPRoleAssignment -List 'Projects' -Principal 'Project Members' -RoleDefinition 'Edit'

5. File Operations & Data Import/Export


Upload a file:
Add-PnPFile -Path 'C:\temp\[Link]' -Folder 'Shared Documents/Reports'
Download a file:
Get-PnPFile -Url '/Shared Documents/Reports/[Link]' -Path 'C:\temp' -FileName '[Link]'
-AsFile'
Import CSV to list (example):
$csv = Import-Csv 'C:\temp\[Link]' foreach ($row in $csv) { Add-PnPListItem -List
'Employees' -Values @{Title = $[Link]; Email = $[Link]} }

6. SPFx Deployment via PowerShell


Automate deploying SPFx packages to the tenant App Catalog and publish them. Ensure package is built
(--ship).
Upload and publish package:
Connect-PnPOnline -Url [Link] -Interactive
Add-PnPApp -Path 'C:\spfx\solution\[Link]' -Scope Tenant -Publish

7. Integration: PowerShell + Power Automate / Power Apps


Use PowerShell to provision sites/lists and then create flows or register apps. You can also call Power
Automate REST API to trigger flows or manage flow definitions via PowerShell. Example: trigger a flow via
HTTP request from PowerShell using Invoke-RestMethod with proper authentication (Azure AD app or
delegated token).
Trigger Flow via HTTP (simplified):
Invoke-RestMethod -Method Post -Uri
'[Link] -Headers @{
'x-functions-key' = '' } -Body ($payload | ConvertTo-Json)

8. Advanced Provisioning & Templates (PnP Provisioning)


PnP Provisioning engine allows you to create templates (XML or PnP) and provision sites consistently.
Use 'Get-PnPProvisioningTemplate' and 'Apply-PnPProvisioningTemplate' for export/import site
templates.
Export template:
Get-PnPProvisioningTemplate -Out '[Link]'
Apply template:
Apply-PnPProvisioningTemplate -Path '[Link]' -Handlers Lists,SiteFields,ContentTypes

9. Monitoring, Reporting & Best Practices


Best practices: use app-only auth for unattended scripts, log actions, use retry and error handling, test in
dev tenancy, avoid storing credentials in scripts, use Key Vault for secrets, and use batching for
performance.
Example: Logging basic results to CSV:
$results | Export-Csv 'C:\temp\[Link]' -NoTypeInformation

10. Exercises (Beginner → Advanced)


Exercise 1 (Beginner): Install [Link], connect to a test site, list all lists and libraries, and upload a
small file. Exercise 2 (Intermediate): Create a PowerShell script to provision a site with 3 lists (Projects,
Tasks, Documents) and create columns; add 10 sample items to Projects. Exercise 3 (Advanced): Build a
provisioning template for a department site, include content types, list templates, and publish a SPFx app
via script. Then create a flow (manual) endpoint and trigger it from PowerShell with sample JSON payload.

11. Quick Cheat Sheet & Useful Links


Cheat Sheet: - Install-Module -Name [Link] - Connect-PnPOnline -Url -Interactive - Get-PnPList
/ New-PnPList / Add-PnPListItem - Add-PnPFile / Get-PnPFile - Add-PnPApp -Path -Publish Useful Links:
- [Link]: [Link] - Microsoft Learn: [Link] - Graph
PowerShell: [Link]
Good luck — practice these scripts in a test tenant. If you want, I can generate additional sample scripts or
a set of runnable .ps1 files.

Common questions

Powered by AI

Uploading a file to a SharePoint Online document library involves using the Add-PnPFile command. This process includes specifying the local file path with the -Path parameter and the target document library's folder path with the -Folder parameter. For instance, Add-PnPFile -Path 'C:\temp\report.pdf' -Folder 'Shared Documents/Reports' will upload 'report.pdf' to the specified library .

Recommended practices include using app-only authentication for unattended scripts to enhance security, logging actions for audit and troubleshooting, applying retry and error handling for robustness, testing scripts in a development tenancy before production use, avoiding storing credentials in scripts, and using Azure Key Vault for managing secrets securely. Additionally, using batching can improve performance when processing large datasets .

Using groups instead of individuals for managing permissions in SharePoint with PowerShell offers scalability and easier management. It allows for bulk assignment and changes to permissions without needing to adjust each user's access individually, which simplifies administration and ensures consistency .

Creating a provisioning script with the PnP.PowerShell module facilitates site setup by allowing administrators to automate the creation and configuration of sites, such as adding lists, libraries, and columns with pre-defined items. This is particularly useful for repetitive tasks like setting up similar project or department sites, reducing manual setup time and ensuring consistency .

To export and log PowerShell script results into a CSV file, use the Export-Csv cmdlet after capturing the results in a variable. For example, once the data is stored in a variable $results, it can be exported to a CSV with Export-Csv 'C:\temp\report.csv' -NoTypeInformation, which is useful for auditing and reporting .

PowerShell can enhance SharePoint automation workflows by integrating with Power Automate through the use of REST API calls. This allows scripts to trigger flows directly from PowerShell, such as using Invoke-RestMethod to initiate a flow via HTTP request, which can automate complex, orchestrated tasks across different services and APIs .

PnP Provisioning Templates optimize site provisioning by allowing repeatable and consistent application of site configurations through templates. Key commands include Get-PnPProvisioningTemplate to export a site configuration into a template, and Apply-PnPProvisioningTemplate to apply a template to a site, ensuring consistent structure and configuration across multiple sites .

The PnP.PowerShell module is recommended over the legacy SPO module for modern tasks because it is cross-platform and more suitable for modern development environments. It supports interactive and app-only authentication methods, making it adaptable for various authentication scenarios, including multi-factor authentication (MFA).

PnP.PowerShell can automate the deployment of SPFx packages by connecting to the tenant app catalog, uploading the SPFx package, and publishing it tenant-wide. This includes steps like building the SPFx package with the --ship flag and using the Add-PnPApp cmdlet to upload and publish the package .

Managing roles and permissions for a SharePoint list using PnP.PowerShell commands involves creating groups with New-PnPGroup, adding users to these groups using Add-PnPGroupMember, and assigning roles to these groups for specific lists using Add-PnPRoleAssignment. For example, granting edit permissions to a group like 'Project Members' on the 'Projects' list is done using these commands .

You might also like