SharePoint PowerShell How To: Create SharePoint Test Documents in 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 documents in root (or any folder) of a document library

Script description: Takes a file located on c:\ and uploads it to a folder in a document library (renaming it automatically on the way up) to generate 1000 documents in the SharePoint library.

# ---- Script settings ---- 
$sourceDocumentPath = "C:\temp\Test.docx" # Source document to spawn new documents from for the creation
$newFilenamePrefix = "Test document "
$newFilenameExtension = ".docx"
$numberDocsToCreate = 1000

# Settings for the destination to create documents in
$webUrl = "http://vs-server38"
$docLibraryName = "Document Set Performance (6000)"
$folderPathWithinDocLibrary = "/Doc Set Load Test 1001" # Leave empty e.g. "" to create documents in root folder of library otherwise specify path relative to root folder e.g. "/Testing/Folder A"

# -------------------------

#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl
$uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary)

#Open file
$file = get-item $sourceDocumentPath
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

# Create documents in SharePoint
write-host "Creating $i documents based on the file $sourceDocumentPath"

for($i=1; $i -le $numberDocsToCreate; $i++)
$newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$i+$newFilenameExtension
write-host "Creating document: $newFilePath ..."
$spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true)

write-host "Completed"

#Close file stream

#Dispose web

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 December 11, 2013, in PowerShell, SharePoint and tagged , , , , , , , , , , , , , , , . Bookmark the permalink. 4 Comments.

Leave a Reply

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

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