SharePoint PowerShell How To: Create SharePoint Library Folders for Load/Performance Testing

SharePoint PowerShell How To Series – Foreword

I often find myself scratching around the local drive of my development machines for remnants of PowerShell scripts to generate list & libraries in SharePoint with all sorts of different folder hierarchies to test the performance of code I’m working on to ensure it scales well once the folder & item counts start getting up to high numbers. So rather than keep scratching I’m going to start posting my scripts as I create and use them so I can find/reuse them later on and someone else might find them useful as well. I’m sure they won’t work under all scenarios and situations (and they are not designed to). They are just quick scripts that serve a specific purpose to me at the time and you may be able to tweak to fit your needs.

Create folders in the root of a document library

Script description: Create 4000 subfolders within the “Folder Performance Test 01” library on the SharePoint Server “vs-server38”

#Script settings
$webUrl = "http://vs-server38"
$listName = "Folder Performance Test 01"
$numberFoldersToCreate = 4000;
$folderNamePrefix = "folder";

# Open web and library
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

# Create desired number of subfolders
for($i=1; $i -le $numberFoldersToCreate; $i++)
{
$folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$folderNamePrefix$i")
$folder.Update()
write-host $i
}

#Dispose web
$web.Dispose()

Create folders within an existing folder in a document library

Script description: Create 4000 subfolders within a folder called “C” within the “Folder Performance Test 02” library on the SharePoint Server “vs-server38”

# Script settings
$webUrl = "http://vs-server38"
$listName = "Folder Performance Test 02"
$subFolderName = "C"
$numberFoldersToCreate = 4000;
$folderNamePrefix = "folder";

# Open web and library
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

# Get handle on the subfolder
$subFolder = $list.RootFolder.SubFolders.Item($subFolderName);
Write-Host $subFolder

# Create desired number of subfolders
for($i=1; $i -le $numberFoldersToCreate; $i++)
{
$folder = $list.AddItem($subFolder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "$folderNamePrefix$i")
$folder.Update()
write-host $i
}

#Dispose web
$web.Dispose()

Related articles

About Cameron Dwyer

Architect and developer at OnePlace Solutions. Passionate about delivering compelling solutions on the Office 365/SharePoint platform. Addicted to coffee.

Posted on November 6, 2013, in PowerShell, SharePoint and tagged , , , , , , , , , , . Bookmark the permalink. 4 Comments.

  1. I am looking to do something similar to your second example, but already have a predefined set of folder names that I want to create. Here is my scenario. In our Office365 Sharepoint, we have a document library named Shared Documents. In that library we have a folder named Clients. We then have a sub folder for each of our clients. We would like to be able to create a consistent set of sub folders and possibly sub sub folders for each client, by first checking if it exists and then if not create them. We would need it to take a variable name for the client name, but everything else would be hard coded. Ideally it would take the client name on the command line as an argument so it would be like open powershell run script.ps1 client name. Is something like that easily doable?

    Like

  1. Pingback: Les dossiers et le seuil d’affichage | KTNN SharePoint

  2. Pingback: How to bulk upload/copy a folder structure and files to SharePoint | Cameron Dwyer | SharePoint, Outlook, OnePlaceMail

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: