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 Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

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