SharePoint Online: Bulk Create Dummy Files using PowerShell

Requirement: For a testing exercise, We had to bulk upload 5000+ documents to a document library in SharePoint Online.

PnP PowerShell to Create Dummy Files in Bulk in SharePoint Online

This PowerShell script bulk uploads the given file with unique names to the root folder of a specified SharePoint Online document library. Make sure you have the Local File “C:\Temp\Dummy.docx” and the SharePoint document library already before running this script.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Dev"
$LibraryName = "LargeLibrary"
$LocalFile= "C:\Temp\Dummy.docx"
$MaxFilesCount = 5000

Try {
    #Get the File
    $File = Get-ChildItem $LocalFile

    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    #$Library = Get-PnPList -Identity $LibraryName

    $FileCounter = 1
    While($FileCounter -le $MaxFilesCount)
    {
	    $NewFileName= $File.BaseName+"_"+$FileCounter+".docx"
	    Try
	    {
		    Add-PnPFile -Path $File -Folder $LibraryName -NewFileName $NewFileName | Out-Null
	    }
	    Catch
	    {
		    Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
	    }
	    Write-host -f Green "New File '$NewFileName' Created ($FileCounter of $MaxFilesCount)!"
	    $FileCounter++
    }
}
Catch {
    write-host -f Red "Error Uploading File:"$_.Exception.Message
}

Based on the number of files and file size, the script’s execution time varies and creates documents on the given document library.

Similarly, to bulk create a large number of list items in SharePoint Online, use: PowerShell to Bulk Add List Items in SharePoint Online using PnP-Batch

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *