Remove Sharepoint folders using Powershell

$WebUrl = "<YOURSITEURL>"

# Create the service to read the copy request list         
$service = New-WebServiceProxy -Uri ($WebUrl + "/_vti_bin/lists.asmx?WDSL")  -Namespace SpWs  -UseDefaultCredential            

# The  name of the list (can also use GUID with curly braces)             

$listName = "{0CE7F2CB-771D-42D8-87EA-20DB5F273B5B}"        

# Create xml query to retrieve records due today. We don't need the full
# document, but it allows us to create the elements that we need for parameters            
$xmlDocToday = new-object System.Xml.XmlDocument

# select
$query = $xmlDocToday.CreateElement("Query")

$viewFields = $xmlDocToday.CreateElement("ViewFields")
$viewFields.set_InnerXml("<FieldRef Name='ID' /><FieldRef Name='FileLeafRef' /><FieldRef Name='Author' /><FieldRef Name='FSObjType' /><FieldRef Name='ItemChildCount' /><FieldRef Name='FolderChildCount' />") 

$queryOptions = $xmlDocToday.CreateElement("QueryOptions") 
$queryOptions.set_InnerXml("<ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' />")

# retrieve the item 

[System.Xml.XmlNode]$ndReturn = $service.GetListItems($listName, $null, $query, $viewFields, "1000", $queryOptions, $null)
$howmany = $ndReturn.data.row.length

 if($howmany -eq $null) {
 Write-Host "No documents"
 }
 else
 {
    Write-Host "Count of items: " $howmany

    foreach ($row in $ndReturn.data.row) {
      Write-Host $row.ows_FSObjType $row.ows_ItemChildCount $row.ows_FolderChildCount $row.ows_FileLeafRef.split(";#")[2]"," created by $row.ows_Author.split(";#")[2]
# do your delete here
    # FSObjType 1 = folder/docset, normal items = 0 , comes in as ID;#1 or ID;#0 item child count is number of items
    # itemchild count ID;#
    # folderchildcount ID;#
    } # end of foreach
 } # end of if statement checking for count

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami