Creating Scheduled Tasks for SharePoint using PnP PowerShell

Loading

Scheduling tasks in SharePoint using PnP PowerShell allows administrators to automate repetitive operations like backups, reports, list updates, permission changes, and document library management. Windows Task Scheduler can execute PnP PowerShell scripts at defined intervals without manual intervention.

This guide covers:
Creating and testing a PowerShell script
Setting up a scheduled task in Windows Task Scheduler
Running the scheduled task with stored credentials
Verifying task execution logs


Step 1: Install and Configure PnP PowerShell

Ensure PnP PowerShell is installed:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force

Connect to SharePoint Online:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteUrl -Interactive

Connected to SharePoint Online!


Step 2: Write a PowerShell Script

Create a script to export a SharePoint List to CSV as an example.

📌 Save this script as C:\Scripts\ExportListData.ps1

# Define Variables
$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$ListName = "Tasks"
$ExportPath = "C:\Backups\TasksBackup.csv"

# Connect to SharePoint Online
Connect-PnPOnline -Url $SiteUrl -UseWebLogin

# Export List Items
$Items = Get-PnPListItem -List $ListName
$Items | Select Title, Status, Created | Export-Csv -Path $ExportPath -NoTypeInformation

Write-Host "List data exported successfully!"

Script ready!


Step 3: Schedule the Task in Windows Task Scheduler

1️⃣ Open Task Scheduler

  • Press Win + R, type taskschd.msc, and hit Enter.

2️⃣ Create a New Task

  • Click Create Basic Task
  • Name it “SharePoint List Export”
  • Click Next

3️⃣ Set the Trigger (When to Run)

  • Choose Daily (or any frequency)
  • Set Time
  • Click Next

4️⃣ Set the Action (What to Run)

  • Select Start a Program
  • Browse to powershell.exe
  • In “Add arguments”, enter:
-File "C:\Scripts\ExportListData.ps1"

5️⃣ Set Security Options

  • Click “Run whether user is logged on or not”
  • Check “Run with highest privileges”

6️⃣ Finish and Save Task

Task Scheduled!


Step 4: Running the Task with Stored Credentials

To avoid manual login, store credentials securely:

1️⃣ Run the following command to encrypt and save credentials:

$Creds = Get-Credential
$Creds | Export-Clixml -Path "C:\Scripts\SP_Credentials.xml"

2️⃣ Modify the Script to Use Stored Credentials

Update C:\Scripts\ExportListData.ps1:

# Load Credentials
$Creds = Import-Clixml -Path "C:\Scripts\SP_Credentials.xml"

# Connect to SharePoint
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Credentials $Creds

# Export List Items
$Items = Get-PnPListItem -List "Tasks"
$Items | Select Title, Status, Created | Export-Csv -Path "C:\Backups\TasksBackup.csv" -NoTypeInformation

Write-Host "List data exported successfully!"

Now, the script runs without manual login!


Step 5: Test and Verify Execution

1️⃣ Run Task Manually

  • Open Task Scheduler
  • Select “SharePoint List Export”
  • Click “Run”

2️⃣ Check Log File for Errors

  • Open Event Viewer (eventvwr)
  • Navigate to Windows Logs > Application
  • Look for PowerShell execution logs

Task executed successfully!

Leave a Reply

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