PowerShell – PnP – Add-PnPFile fails with “Access denied” resolving the error

fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error
fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error

Hi All,

Greetings for the day!!! Today new issue and resolution 🙂

Issue Getting “Access denied” error while executing Add-PnPFile PowerShell CMDLET

Details / Background

  • In our one of the SharePoint online project, we are executing PowerShell script
  • One of the requirement is to upload few files in “Site Assets” library
  • We were implementing this requirement using PowerShell and using “Add-PnPFile” CMDLET
  • While executing “Add-PnPFile” CMDLET we are getting “Access denied” error

Issue / Error

PS C:\> Add-PnPFile -Path “C://Users//Documents//PS//ApplyTemplate//images//hideRepublishButton.html” -Folder “SiteAssets”
Add-PnPFile : Access denied.

  • Add-PnPFile -Path “C://Users//Documents//PS//ApplyTe …
  • ~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [Add-PnPFile], PSInvalidOperationException
    • FullyQualifiedErrorId : InvalidOperation,PnP.PowerShell.Commands.Files.AddFile

fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error
fig : PowerShell – Executing CMDLET “Add-PnPFile” – “Access denied” error

Reason / Cause

fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error - Permissions page - /_layouts/user.aspx
fig : PowerShell – Executing CMDLET “Add-PnPFile” – “Access denied” error – Permissions page – /_layouts/user.aspx
  • To check my permissions I clicked on “PERMISSIONS” tab as
fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error - Checking permissions of user
fig : PowerShell – Executing CMDLET “Add-PnPFile” – “Access denied” error – Checking permissions of user
  • Click on “Check Permissions” ribbon button as shown in above fig
  • On click of “Check Permissions” ribbon button we will have “Check Permissions” dialog as
fig : PowerShell – Executing CMDLET “Add-PnPFile” – “Access denied” error – Checking permissions of user
  • In above figure if we notice “Check Permissions” dialog – check the last line – “Deny” for :Add and Customize Pages” and this is the issue.

Solution

  • To make it work we can update site collection level property – DenyAddAndCustomizePages
  • By default value for DenyAddAndCustomizePages property is Enables, we need to disable it
fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error - verifying site collection property - DenyAddAndCustomizePages

  • Updating Site Collection property – using PowerShell we will use “Set-SPOSite” PowerShell CMDLET as

# Updating site collection level property - "DenyAddAndCustomizePages" using "Set-SPOSite" CMDLET
Set-SPOSite -DenyAddAndCustomizePages $false -Identity https://knowledgejunction1.sharepoint.com/sites/sitewithpowershell

#once updated successfully, verifying the respective property
Get-SPOSite -Identity https://knowledgejunction1.sharepoint.com/sites/sitewithpowershell |fl -Property DenyAddAndCustomizePages
DenyAddAndCustomizePages : Disabled

fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error - updating site collection property - DenyAddAndCustomizePages - using Set-SPOSite
fig : PowerShell – Executing CMDLET “Add-PnPFile” – “Access denied” error – updating site collection property – DenyAddAndCustomizePages – using Set-SPOSite

  • After successfully updating property, we will able to upload the respective files

PS C:\>  Add-PnPFile -Path "C://Users//Documents//PS//ApplyTemplate//images//hideRepublishButton.html" -Folder "SiteAssets"

Name                     Type Items/Size Last Modified       
----                     ---- ---------- -------------       
hideRepublishButton.html File        283 7/30/2023 8:14:03 AM

fig : PowerShell - Executing CMDLET "Add-PnPFile" - "Access denied" error - After updating site collection property - DenyAddAndCustomizePages - using Set-SPOSite successfully, Add-PnPFile CMDLET executed successfully
fig : PowerShell – Executing CMDLET “Add-PnPFile” – “Access denied” error – After updating site collection property – DenyAddAndCustomizePages – using Set-SPOSite successfully, Add-PnPFile CMDLET executed successfully

REFERENCES

Allow or prevent custom script

Thanks for reading ! HAVE a FANTASTIC LEARNING AHEAD !! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: