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
- SharePoint PowerShell How To: Create SharePoint List Items (in Root and in Folders) for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Test Documents in Library Folders for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Document Sets for Load/Performance Testing
- SharePoint PowerShell How To: Create SharePoint Library Folders for Load/Performance Testing
- How to import (upload) an entire folder of files to SharePoint using PowerShell
Reblogged this on Sutoprise Avenue, A SutoCom Source.
LikeLike
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?
LikeLiked by 1 person
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.
LikeLiked by 1 person
Hi Joe. I totally missed Darrel’s comment way back in 2015. Creating a client folder (with name based on parameter) plus a defined list of sub folders is certainly possible and would be fairly straight forward. Take a look at this link for breaking permissions on your folders and setting unique ones via PowerShell
https://sharepoint.stackexchange.com/questions/93278/setting-permission-on-folders-and-items-of-document-library
LikeLike