use PowerShell to create a bunch of folders

I’m going to explain this idea in two posts.  This post will outline creating a bunch of folders in a Document Library.  After the folders are created, I will then demonstrate how to put a bunch of Word documents in the newly created folders.

Why?

I was needing a way to stress test / performance test updating documents in SharePoint.


if(-not(Get-PSSnapin | where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$site = Get-SPweb "http://sharepointed.com/taco/"
$mList = $site.Lists["Shared Documents"]

$range = 1..1000

$count = $range.Count
for($i=0; $i -lt $count; $i++)
 {
 $spFolder = $mList.AddItem("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$i)
 $spFolder.Update()
 }

$site.dispose()

$range can be adjusted to create less or more folders.  If you only wanted to create ten folders, change $range = 1..10

I’m using the incremented value of $i to name the folders. You can setup a another naming convention for the foldering.

In the next post, I will populate the folders with a Word document.

Leave a Reply

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