Use PowerShell PNP to Create an Alphabetical Directory of Folders in SharePoint

I’m in the process of reorganizing a document library and wanted to store all of the documents in alphabetical folders. Yes, I’m using metadata, but I’ve passed the magic 5,000 item threshold and want to rearrange the library and leverage a rich search experience.

So, using PowerShell, how do you create a bunch of folders going from A to Z?

$siteURL = "https://sharepointed.sharepoint.com/sites/parent/child"

$conn = Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential) -ReturnConnection

try{
(65..(65+25)).ForEach({     
$xy = [char]$_    
Add-PnPFolder -Name $xy -Folder "/mylibrary" -Connection $conn
})
}

catch{ Write-host -f Red "Error:" $_.Exception.Message}

More information about creating folders using ASCII:
https://devblogs.microsoft.com/scripting/use-powershell-and-ascii-to-create-folders-with-letters/

use PowerShell to create a bunch of folders

I’m going to explain this idea in two posts.  This post will outline creating a bunch of folders in a Document Library.  After the folders are created, I will then demonstrate how to put a bunch of Word documents in the newly created folders.

Why?

I was needing a way to stress test / performance test updating documents in SharePoint.


if(-not(Get-PSSnapin | where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$site = Get-SPweb "http://sharepointed.com/taco/"
$mList = $site.Lists["Shared Documents"]

$range = 1..1000

$count = $range.Count
for($i=0; $i -lt $count; $i++)
 {
 $spFolder = $mList.AddItem("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$i)
 $spFolder.Update()
 }

$site.dispose()

$range can be adjusted to create less or more folders.  If you only wanted to create ten folders, change $range = 1..10

I’m using the incremented value of $i to name the folders. You can setup a another naming convention for the foldering.

In the next post, I will populate the folders with a Word document.