Wednesday, September 28, 2016

SharePoint Online: How to Delete a Folder using PowerShell

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
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.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...