Especially useful if you need to approve a lot of files in a batch. Works for approving SharePoint pages, news items, or library files.
Get pages
First let us get the pages from the library and view their approval status.
Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/yoursite -Interactive -ClientId "b8d9-yourappid-709de"
Get-PnPListItem -List "Site Pages" | Select-Object @{Name="FileRef";Expression={$_.FieldValues["FileRef"]}},
@{Name="_ModerationStatus";Expression={$_.FieldValues["_ModerationStatus"]}},
@{Name="PromotedState";Expression={$_.FieldValues["PromotedState"]}}, Id
Approve a page
In order to approve a news or a page in SharePoint, you need to set _ModerationStatus property to 0.
Set-PnPListItem -List "Site Pages" -Identity 1090 -Values @{"_ModerationStatus" = "0"}
There are 4 values for the Moderation Status:
0 - Approved
1 - Rejected
2 - Pending
3 - Draft
Approve all pages
# Connect to SharePoint
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive -ClientId "b8d9-yourappid-709de"
# Get all items from the "Site Pages" library
$pages = Get-PnPListItem -List "Site Pages"
# Loop through each item and approve it
foreach ($page in $pages) {
$itemId = $page.Id
Write-Host "Approving page with ID: $itemId"
Set-PnPListItem -List "Site Pages" -Identity $itemId -Values @{"_ModerationStatus" = "0"}
}
Write-Host "All pages have been approved"
Reject a page
In order to reject a news or a page in SharePoint, you need to set _ModerationStatus property to 1.
Set-PnPListItem -List "Site Pages" -Identity 1090 -Values @{"_ModerationStatus" = "1"}
Reject all pages
# Connect to SharePoint
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Get all items from the "Site Pages" library
$pages = Get-PnPListItem -List "Site Pages" -Fields "FileLeafRef"
# Loop through each item and reject it
foreach ($page in $pages) {
$itemId = $page.Id
$fileName = $page.FieldValues["FileLeafRef"]
Write-Host "Rejecting page: $fileName (ID: $itemId)"
Set-PnPListItem -List "Site Pages" -Identity $itemId -Values @{"_ModerationStatus" = "1"}
}
Write-Host "All pages have been rejected"