We have discovered a library where a user has written script to create auto numbered folders, within a library... we now have 32000 of them!!?

I need a powershell script that will list the folders, and ideally tell me if they are empty, so they can be deleted.

I've looked at numerous example scripts listing the contents of libraries, but none really target the folders specifically, more over the content within the folder.

I originally had a script written against a sample smaller library, using the UNC path to the library but this doesn't work for this library.. I'm guessing because of the number of folders it contains...

Any ideas, gratefully received..... I'm stuck.

  • 1
    which version are you using?
    – Mike
    Commented Nov 10, 2014 at 19:02

3 Answers 3


This one provides a little more info than Mike's (again, replace web URL and list name). I'm outputting to a text file because you said there's 32000 folders, I doubt you want to scroll through 32000 lines of window buffer:

$TestSPSnapin = get-pssnapin | ? { $_.Name -eq "Microsoft.SharePoint.Powershell"} 
if($TestSPSnapin -eq $null){
    add-pssnapin -Name Microsoft.SharePoint.Powershell #-ErrorAction SilentlyContinue
    # SilentlyContinue is only good for interactive console where you're sure of each command you're running. Otherwise, the script should block execution on snapin load failure.

function CheckFolderContents ($folder)
    $folderContent = "Folder at URL " + $folder.Url + " has " + $folder.Files.Count + " files and " + $folder.SubFolders.Count + " subfolders.`n"
    Add-Content C:\FolderContents.txt $folderContent

    foreach($subfolder in $folder.SubFolders)
        if ($subfolder.item -ne $null) # the default "Forms" folder has a null Item, and we don't need to include the default "Forms" folder as part of this
            CheckFolderContents $subfolder


$web = Get-SPWeb http://server/site/web

$list = $web.Lists["Name of List"]

CheckFolderContents $list.RootFolder

  • If you have tested that the PSSnapin isn't loaded, then why -ErrorAction SilentlyContinue? If the snapin fails to load, the script won't work and might even misbehave if appended with more local code without sufficient care. Commented Nov 10, 2014 at 20:18

Try this script, replacing the listname and weburl :

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue    
$web = Get-SPWeb http://sharepoint/sitecoll/web
$list = $web.Lists["List Name"]

    foreach($folder in $list.Folders)
      if($folder.Folder.SubFolders -eq $null)
       Write-Host Empty folder at $folder.Url

In SharePoint 2010 or later, the Folder content type has ItemChildCount and FolderChildCount columns that can be sorted or filtered by in a view or by an SPQuery <Where/> clause.

That way you don't have to write any PowerShell at all to find the empty ones - just look for 0 in both columns, that's all.

Later on you can write a simple JavaScript do automate deleting from such a view batched by 100 items with the help of SP.ListOperation.Selection type's operations, which would be faster than calling SPListItem.Delete() from PowerShell (well, you could write a CAML batch in PowerShell too, but it's not that pretty.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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