Implementing SharePoint Online Content Scheduling using PnP PowerShell

Loading

Content scheduling in SharePoint Online allows you to publish or archive content automatically at predefined times. Automating this process using PnP PowerShell helps:

Schedule content for future publishing
Archive outdated documents automatically
Ensure compliance and content governance

With PnP PowerShell, we can:
Enable content scheduling
Set publish/unpublish dates
Automate archival of expired content


Step 1: Connect to SharePoint Online

Run the following command to securely connect to your SharePoint site:

$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $siteUrl -Interactive
Write-Host " Connected to SharePoint Online"

✔ Establishes a secure connection.


Step 2: Enable Content Scheduling for a Library

To schedule publishing, we must first enable major/minor versioning and approval:

$libraryName = "Pages"
Set-PnPList -Identity $libraryName -EnableModeration $true -EnableVersioning $true -MajorVersions 10 -EnableMinorVersions $true
Write-Host " Content scheduling enabled for '$libraryName'"

✔ Ensures content follows approval workflows.
✔ Allows drafts before publishing.


Step 3: Add Publish and Expiry Date Columns

If these columns do not exist, create them:

Add-PnPField -List $libraryName -DisplayName "Publish Date" -InternalName "PublishDate" -Type DateTime -AddToDefaultView
Add-PnPField -List $libraryName -DisplayName "Expiry Date" -InternalName "ExpiryDate" -Type DateTime -AddToDefaultView
Write-Host " Publish and Expiry Date fields added"

✔ Helps schedule content visibility.


Step 4: Automate Content Publishing

Check for items scheduled to be published and publish them:

$scheduledItems = Get-PnPListItem -List $libraryName | Where-Object { $_["PublishDate"] -and (Get-Date) -ge $_["PublishDate"] }

foreach ($item in $scheduledItems) {
Set-PnPListItem -List $libraryName -Identity $item.Id -Values @{"_ModerationStatus"=0}
Write-Host " Published: $($item.FieldValues["FileLeafRef"])"
}

✔ Publishes scheduled content automatically.


Step 5: Automate Content Expiry (Archival)

Check for expired content and move it to an archive:

$expiredItems = Get-PnPListItem -List $libraryName | Where-Object { $_["ExpiryDate"] -and (Get-Date) -ge $_["ExpiryDate"] }

foreach ($item in $expiredItems) {
Move-PnPFile -ServerRelativeUrl $item["FileRef"] -TargetUrl "/sites/YourSite/ArchivedContent" -Force
Write-Host " Archived: $($item.FieldValues["FileLeafRef"])"
}

✔ Moves expired files to archive folders.


Step 6: Automate the Script (Task Scheduler)

Schedule the script to run daily:

$taskAction = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\ContentScheduling.ps1"
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 2AM
Register-ScheduledTask -TaskName "SharePoint Content Scheduling" -Action $taskAction -Trigger $taskTrigger -RunLevel Highest

✔ Automates content management.

Leave a Reply

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