kwizcom banner advertisement

SharePoint Online: How to Delete a Folder using PowerShell?

Requirement: SharePoint Online PowerShell to Delete Folder
SharePoint Online PowerShell to Delete Folder

How to delete a folder in SharePoint Online:
Simply select the folder from list or library, and then you can delete a folder either from toolbar "delete" button or from the context menu's delete option.
SharePoint Online PowerShell Delete Folder
Confirm the delete prompt and you are done!

SharePoint Online PowerShell to Delete Folder
Here is the PowerShell to remove a folder from list or library in SharePoint Online.
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Projects"
$FolderURL="Shared Documents/Project 2020"

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
    $Web = $Ctx.Web

    #Get the folder object from given URL
    $Folder=$web.GetFolderByServerRelativeUrl($FolderURL)
    
    #Delete the folder
    $Folder.DeleteObject()
    $Ctx.ExecuteQuery()

    Write-host "Folder deleted Successfully!" -ForegroundColor Green
}
Catch {
    write-host -f Red "Error deleting Folder!" $_.Exception.Message
}

Similarly, you can delete any sub-folder by setting the FolderURL parameter in the above script.
SharePoint Online: How to Delete a Folder using PowerShell? SharePoint Online: How to Delete a Folder using PowerShell? Reviewed by Salaudeen Rajack on 1:28 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.