Microsoft 365 : SharePoint Online – PowerShell script to delete file using PnP PowerShell

PnP PowerShell to remove SharePoint online file
PnP PowerShell to remove SharePoint online file

Hi All,

Greetings for the day !!!

Today sharing one more PowerShell script – script to delete SharePoint online file using PnP PowerShell

Background / Use Case

  • Our security team noticed that one important file is shared across multiple sites
  • Our team has scanned and found all the sites and respective file locations
  • CSV file is prepared
  • Next our task is scan through CSV file, get the file location and delete the file
  • So we have prepared the PowerShell script, we have used PnP PowerShell

Takeaway

  • How to connect to PnP online using PowerShell
  • At the end of this article we got to know how to delete respective file using PnP PowerShell

Prerequisites

  • PnP online PowerShell module is installed
  • If PnP online PowerShell module is not installed then please download latest PowerShell module and install
  • You could install PowerShell PnP module using Install-Module CMDLET

Install-Module PnP.PowerShell

    Details / Steps

    • Connect to site using PnP PowerShell – using Connect-PnPOnline

    #connect to PnP online - connect the site from which we need to delete the file
    
    Connect-PnPOnline -Url https://knowledgejunction1.sharepoint.com/sites/Demo -Interactive

    • Verify if file exists – using Get-PnPFile

     #Verify if file exists
    
        $file = Get-PnPFile -Url /sites/Demo/Shared%20Documents/fig1.png
    
        if($file){
           write-host "File exists"
        }

    • If file exists – delete the file. Here we are sending the file Recycle Bin using -Recycle parameter to CMDLET – Remove-PnPFile
    • -Force attribute is used to bypass the dialog – file deletion confirmation

    Remove-PnPFile -SiteRelativeUrl "/Shared Documents/fig1.png" -Recycle -Force

      Complete Script

      #import PnP PowerShell module
      Import-Module PnP.PowerShell -DisableNameChecking
      
      try{
          #connect to PnP online - connect the site from which we need to delete the file
          Connect-PnPOnline -Url https://knowledgejunction1.sharepoint.com/sites/Demo -Interactive
      
          #Verify if file exists 
          $file = Get-PnPFile -Url "/sites/Demo/Shared Documents/backcover.jpg"
      
          if($file){
                  #send file to recycle bin - using -Recycle parameter
            Remove-PnPfile -SiteRelativeUrl "/Shared Documents/backcover.jpg" -Recycle -Force
            Write-Host "File /sites/Demo/Shared%20Documents/backcover.jpg deleted 
                        successfully" -ForegroundColor Green 
           }#if($file)
          }#try
          catch{
              Write-Host "Exception occurred : $($_.Exception.Message)" -ForegroundColor Red
          }#catch

      References

      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.

      Discover more from Microsoft 365

      Subscribe now to keep reading and get access to the full archive.

      Continue reading