I want to delete particular folders from SharePoint Document library.

I have done this using C#, Its working fine. But I want to modify the same code to powershell.

i tried below code with powershell. I want to modify this C# to Powershell Code I am having doubt in this line:

string documentLibraryFolderurl= SPContext.Current.Web.Url + "/" + oList + "/" + str + "/" + "Test" + "/" + CategoryName;
if (oWeb.GetFolder(documentLibraryFolderurlAward).Exists == true)
SPFolder folder = oWeb.GetFolder(documentLibraryFolderurlAward);

can anyone help me on this?


I'll assume that you know the web you want to get... Using SPContext is possible with a bit of munging but in my opinion is not required in most PS scripts. So your PS should look like:

#Get the web
$web = Get-SPWeb http://SharePoint/site/web
#Get the list. You don't need the list if you know the list path just pass it as a string instead.
$list = $web.Lists.TryGetList("Documents")
if ($list) {
    #I'm assuming you have the $str and $categoryName vars. It looks much the same if you want:
    $folder = $list.Rootfolder.ServerRelativeUrl + "/" + $str + "/" + "Test" + "/" + $CategoryName
    #or it could simply be:
    $folder = "$($list.RootFolder.ServerRelativeUrl)/$str/Test/$CategoryName"
    #Then get the folder:
    $folder = $web.GetFolder($folder)
    if ($folder.Exists) {
| improve this answer | |
Add-PSSnapin Microsoft.SharePoint.PowerShell    

$web = Get-SPWeb -Identity "http://thegowtham.sharepoint.com"    

$listname = $web.GetList("http://thegowtham.sharepoint.com /Library")    

function DeleteFiles   
  $folder = $web.GetFolder($folderUrl)      
  foreach ($file in $folder.Files)   
    # Delete file by deleting parent SPListItem    
    Write-Host("DELETED FILE: " + $file.name)    


# Delete root files    


# Delete files in folders    

foreach ($folder in $listname.Folders)   

# Delete folders    

foreach ($folder in $list.Folders)   
    Write-Host("DELETED FOLDER: " + $folder.name)    
    Write-Host(“Deletion of parent folder already deleted this folder”)    
| improve this answer | |
  • I want to delete particulae folders using folder names if( $spFolder.Name = "Test") $splList.Folders.DeleteItemById($spFolder.ID) I was running powershell script error is coming "'Name' is a ReadOnly property." – elina Oct 23 '17 at 9:42
  • The correct condition would be if( $spFolder.Name == "Test") (double ==) – Aakash Maurya Sep 25 '18 at 7:18

We can use the below PnPPowerShell Powershell commands to delete the folder from SharePoint library

PS:> Remove-PnPFolder -Name FolderNametobeDelete -Folder "Shared Document/ParentFolder"

The PnPPowerShell is available from the below URL, https://github.com/SharePoint/PnP-PowerShell . We have to install PnPPowerShell modules based on the SharePoint version.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.