Mass Create Word Documents Using PowerShell

I’m my other post,  I outlined how to create a bunch of folders in a Document Library.  In this post, I’ll explain how I then populated those folders with Word documents.

To stage this, I created a Document Library called BaseLibrary.  In BaseLibrary I uploaded a single Word document.  After uploading the document, make note of the ID of the document in the Library.  *To get the ID of the document, modify your default View, and add the ID column, click save.*


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

$site = Get-SPweb "http://sharepointed.com/taco"

$spListFrom = $site.lists["BaseLibrary"]
$iFrom = $spListFrom.GetItemById("1")

$mList=$site.Lists["Shared Documents"]

foreach($f in $mlist.Folders)
 {

$fName=$f.Name

$range = 1..500
 $count = $range.Count
 for($i=0; $i -lt $count; $i++)
 {
 $nName = "$fName$i.docx"
 $nURL = $site.Url+''+$f.Url+"/"+$nName
 $iFrom.CopyTo($nURL)
 }
 }

$site.dispose()

What I’m doing here is referencing the document with the ID of 1, from BaseLibrary.

Loop through all the folders in Shared Documents, in each folder, I’m doing a loop to copy the document into the folder, and create a new name for the document on each loop.

$nName = “$fName$i.docx”

$fname is the name of the folder we are working with.

$i is the count of the loop we are on.  Joining the two variables will create a unique file name.

$range = 1..500

To create more or less documents in each folder, adjust this range.  This example will create 500 documents in each folder.