Create Thousands of Folders in SharePoint 2010 using PowerShell

I was recently trying to figure out a MOSS 2007 AND SharePoint 2003 backwards-compatible way of detecting whether a folder exists in a document library with thousands of items, from a remote location, using Out Of The Box web services… don’t ask why…!

Unless I’ve missed something, it’s not possible (apart from the slightly hacky HTTP request to check if the URL exists or if a 404 is returned..):

Lists.asmxGetListItems requires a view, which has a default limit of 5,000… so no good for large document libraries

DspSts.asmx – Whilst deprecated in 2010 in favour of the Client Object Model (for good reason..) the QueryMethod is actually really flexible.. sending CAML via the web service, BUT… it isn’t folder-aware…

In order to test both of the above, and to test various other ways of ensuring any successful method also works on document libraries of a certain size, I needed thousands of folders in my local document library – PowerShell to the rescue!

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null;
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://sp2010");
$OpenWeb = $SpSite.OpenWeb("/docs");
$OpenList = $OpenWeb.Lists["Documents"];

$i = 1
While ($i -le 10000)
    $folder = "Folder " + $i

    $item = $OpenList.Folders.Add("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$folder);
    $folder + " Created!";


2 thoughts on “Create Thousands of Folders in SharePoint 2010 using PowerShell

  1. This did NOT work for me. Errors:
    You cannot call a method on a null-valued expression.
    At line:14 char:17
    + $item.Update <<<< ();
    + CategoryInfo : InvalidOperation: (Update:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Leave a Comment Yourself

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