kwizcom banner advertisement

SharePoint Online: How to Delete a File from Document Library using PowerShell

Requirement:  Delete a file from SharePoint Online document library using PowerShell

SharePoint Online: PowerShell to Delete a File from Document Library
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Remove-SPOFile()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $FileRelativeURL
    )
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the file to delete
        $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()
                
        #Delete the file
        $File.DeleteObject()
        $Ctx.ExecuteQuery()

        write-host -f Green "File has been deleted successfully!"
     }
    Catch {
        write-host -f Red "Error deleting file !" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$FileRelativeURL="/sites/Ops/Shared Documents/Investment Proces1.pptx"

#Call the function 
Remove-SPOFile -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL

This script deletes the file from given URL. There are scenarios where you may have to delete a file using its item id. Here is the another script to remove a document using its item ID in SharePoint Online.

Delete a File from SharePoint Online Library using PowerShell:
This time, lets delete a document from SharePoint Online using its Item id.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Remove-SPOFile()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $LibraryName,
        [Parameter(Mandatory=$true)] [string] $ItemID
    )
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the list by title
        $List=$Ctx.web.Lists.GetByTitle($LibraryName)
        
        #Get the item to delete by ID
        $Item=$List.GetItemById($ItemID)
        #Get the file to delete
        $File=$Item.File

        #Delete the file
        $File.DeleteObject()
        $Ctx.ExecuteQuery()

        write-host -f Green "File has been deleted successfully!"
     }
    Catch {
        write-host -f Red "Error deleting file !" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$LibraryName="Documents"
$ItemID="16"

#Call the function 
Remove-SPOFile -SiteURL $SiteURL -LibraryName $LibraryName -ItemID $ItemID
SharePoint Online: How to Delete a File from Document Library using PowerShell SharePoint Online: How to Delete a File from Document Library using PowerShell Reviewed by Salaudeen Rajack on 3:12 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.