I was working on a customer problem and needed a very large list to try to reproduce a problem. I typically fire up Visual Studio and create Yet Another Console App. You know, one of the hundred or so “ConsoleApplication1” projects that litter your My Documents/Visual Studio/Projects folder? I decided to force myself to do this with PowerShell instead, and the result was not only quick, but also something I could do on a machine that doesn’t have Visual Studio installed.
Not only did I need many items in the list, I also needed a weird structure… 10 folders, each having 10 folders, each having 10 folders, each having 50 items.
Creating a folder in SharePoint is easy, but not straightforward. You have to use the SPFileSystemObjectType.Folder enumeration value when creating a list item. You can see the syntax for that below. Since we want to create nested folders, you can see how we use the relative URL to accomplish that as well.
Here is the script I came up with. Note! Before you run this in your environment, understand that it creates 50,000 items! You might want to change the number of loops to reduce the number of items being created, or better yet, just read the code and adapt it for your situation.
$spAssignment = Start-SPAssignment
$mylist = (Get-SPWeb -identity http://portal.sharepoint.com -AssignmentCollection $spAssignment).Lists["LargeList"]
for($i=1; $i -le 10; $i++)
$folder = $mylist.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$folder["Title"] = "folder$i"
for($j=1; $j -le 10; $j++)
$s1folder = $mylist.AddItem($folder.Folder.ServerRelativeUrl,
$s1folder["Title"] = "subfolder$j"
for($k=1; $k -le 10; $k++)
$s2folder = $mylist.AddItem($s1folder.Folder.ServerRelativeUrl,
$s2folder["Title"] = "subsubfolder$k"
for($l=1; $l -le 50; $l++)
$newItem = $mylist.AddItem($s2folder.Folder.ServerRelativeUrl,
$newItem["Title"] = "Item $i $j $k $l"
$newItem["FirstName"] = "FirstName $i $j $k $l"
$newItem["LastName"] = "LastName $i $j $k $l"
$newItem["Company"] = "Company $i $j $k $l"
That’s it, once you read the code you can see there’s not much to it. And now you’ll be able to get rid of all those throw-away ConsoleApplication1 projects in your My Documents folder!
Creating SharePoint Folders and Items with PowerShell