Skip to content

Instantly share code, notes, and snippets.

@IAMPetro
Last active January 21, 2021 09:49
Show Gist options
  • Save IAMPetro/264be46241396d621a2a50d19b47e6b1 to your computer and use it in GitHub Desktop.
Save IAMPetro/264be46241396d621a2a50d19b47e6b1 to your computer and use it in GitHub Desktop.
SharePoint: Bulk Folder Creation
function Create-SPFoldersBulk
{
<#
.Synopsis
This function will create a folders and subfolders based on the information contained in the imported CSV file
.Description
This function will create a folders and a single level of subfolders based on the information contained with the imported CSV file.
The folder hierarchy is limited to ParentFolder\SubFolderX
.Example
C:\> Create-SPFoldersBulk –SiteUrl "http://yourdomain.com/sites/Finance" –LibraryName "Reports" –CSVPath "C:\Script_Files\folderList.csv"
This example will create parent and subfolders within the Finance Departments 'Reports' library.
.Notes
Name: Create-SPFoldersBulk
Author: Petro Margaritis
Last Edit: 04/07/2012
Keywords: Folder, List Item
.Link
http://www.iampetro.com/
#>
Param (
[parameter(Mandatory=$true)][string]$SiteUrl,
[parameter(Mandatory=$true)][string]$LibraryName,
[parameter(Mandatory=$true)][string]$CSVPath
)
# Import CSV
[array]$FolderList = Import-Csv $CSVPath
# Script Variables
$spWeb = Get-SPWeb $SiteUrl
$spList = $spWeb.Lists[$LibraryName]
Foreach ($folderGroup in $FolderList) {
# Create Parent Folder
$parentFolder = $spList.Folders.Add("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$folderGroup.Root)
$parentFolder.Update()
Write-Host "The Parent folder" $folderGroup.Root "was successfully created" -foregroundcolor Green
# Loop variables
$i = 1
$col = "S"
$colID = $col + $i
While ($folderGroup.$colID -ne $null)
{
$newSubfolder = $splist.Folders.Add($parentFolder.Folder.ServerRelativeUrl,
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$folderGroup.$colID)
$newSubfolder.Update()
Write-Host "The subfolder" $folderGroup.$ColID "was successfully created" -foregroundcolor Cyan
$i++
$colID = $col + $i
}
}
$spWeb.Dispose()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment