Creating SharePoint Folders and Items with PowerShell

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.

image

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 https://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"
    $folder.Update();
    for($j=1; $j -le 10; $j++)
    {
        $s1folder = $mylist.AddItem($folder.Folder.ServerRelativeUrl, 
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
        $s1folder["Title"] = "subfolder$j"
        $s1folder.Update();
         for($k=1; $k -le 10; $k++)
        {
            $s2folder = $mylist.AddItem($s1folder.Folder.ServerRelativeUrl, 
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
            $s2folder["Title"] = "subsubfolder$k"
            $s2folder.Update();
                        
            for($l=1; $l -le 50; $l++)
            {
                
                #Create item
                
                $newItem = $mylist.AddItem($s2folder.Folder.ServerRelativeUrl,
[Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
                $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"
                $newItem.Update()
            }
        }   
    }
}
Stop-SPAssignment $spAssignment

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!

For More Information

Creating SharePoint Folders and Items with PowerShell