MOSS Forum

Ask Question   UnAnswered
Home » Forum » MOSS       RSS Feeds

PowerShell Script for populating a SharePoint library

  Asked By: Harshita    Date: Mar 15    Category: MOSS    Views: 3189

I found a powershell script online for populating a document library with test

(I know there are other ways to stress test a SharePoint farm but this isn't for
stress testing.)
For some reason though it doesn't work. I was wondering if someone could tell
me how to get this to work, correctly.
What am I missing?

function global:Add-Documents
#collect command parameters
$webUrl=$(Throw("You must specify the URL for the web to add documents to")),
$libraryName=$(Throw("You must specify the library to add documents to"))
#create folder containing files to upload
New-Item -type directory -Path c:\UploadFiles

#Create files to upload in
1..10000 | New-Item C:\UploadFiles\file$_.txt -type file -Value "This is the
file content for file $_"

#assign variables to script
$web = get-SPweb($webUrl)
$spList = $web.Lists[$libraryName]
$docLibFolder = $web.GetFolder($libraryName);

#get all files from the folder
$templates = get-childitem "C:\UploadFiles"
WRITE-HOST -FOREGROUND CYAN "Retrieved files..."

#copy all generated files to library
foreach($template in $templates)
WRITE-HOST -FOREGROUND CYAN "Getting byte[] for $template ..."
$bytes = get-content $template.FullName -encoding byte
$docLibFolder.Files.Add($template.Name,$bytes, $true) > $null
WRITE-HOST -FOREGROUND CYAN "Added $template.Name successfully..."

#cleanup on aisle 3
Write-Host -ForegroundColor Cyan "Completed adding test files to



3 Answers Found

Answer #1    Answered By: Daniella Landry     Answered On: Mar 15

Are there any error messages or other effects that might indicate what's
going on? Anything in either the event log or the SharePoint (12 hive) log?

Answer #2    Answered By: Bo Stafford     Answered On: Mar 15

I do get error messages,

New-Item : The input object cannot be bound to any parameters for the command
either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At C:\Library Populator.ps1:22 char:21
+ 1..10000 | New-Item <<<< C:\UploadFiles\file$_.txt -type file -Value
"This is the file content for file $_"
+ CategoryInfo : InvalidArgument: (1:Int32) [New-Item],
+ FullyQualifiedErrorId :

Not sure what is wrong with the new-item command.

Answer #3    Answered By: Jocelyn Shelton     Answered On: Mar 15

I'm just learning PowerShell, so although my suggestion will work (I tested it)
it may not be the best, most succinct syntax. Change the line that is giving an
error to:

foreach ($num in 1..10000) {New-Item C:\UploadFiles\file$num.txt -type file
-Value "This is the file content for file $num"}

Didn't find what you were looking for? Find more on PowerShell Script for populating a SharePoint library Or get search suggestion and latest updates.