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

6 thoughts on “SharePoint PowerShell How To: Create SharePoint Library Folders for Load/Performance Testing

Add yours

  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?

    Liked by 1 person

    1. this is exactly what i need to do but i need to set non inherited permissions on those subfolders to control access to some and leave some open and inherited.

      Grrr yes these are complex questions.

      Liked by 1 person

Leave a Reply to Darrell Null Cancel 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: