Skip to content

Upload Files to Library in SharePoint 2013 using PowerShell

One of the most common questions I see on various forums is on “How to upload files to SharePoint Library?” and to a larger extent of these questions are related to a scheduled upload of files.

Thus, the problem statement becomes “How to schedule upload of files to a SharePoint Library?”.

You can always do it by writing a SharePoint Timer Job using .Net code and deploying the timer job to your SharePoint farm. Wait a second, did we just say deploy to farm? So, does it makes sense to have a new timer job running at farm level for a such a common, simple problem?

Let’s take it to another extent and just assume that we want to have our solution cloud ready so that if we want to move to Office 365 in future, that’s ready for it as well.

Hold on, there is no timer job in Office 365. So what do we do now? Jamie McAllister discussed this scenario in his post Alternatives to SharePoint Timer Jobs and he proposed a nice design to use Windows Scheduler as an alternative of Timer Jobs. Kind of makes sense that you don’t want to overload of SharePoint environment with all these small tasks/timer jobs and have them maintained outside your SharePoint environment. In fact, it is not responsibility of SharePoint Farm to upload files but of a client (application) to do that at a time (interval) when it wants.

Following the same principle, here is a PowerShell script which you can use to upload files to SharePoint.

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

#Site Collection where you want to upload files
$siteCollUrl = "http://webapplication/sites/Upload"
#Document Library where you want to upload files
$libraryName = "Shared Documents"
#Physical/Network location of files
$reportFilesLocation  = "C:\Users\manas\Desktop\Files"

$spSourceWeb = Get-SPWeb $siteCollUrl;
$spSourceList = $spSourceWeb.Lists[$libraryName];

if($spSourceList -eq $null)
	Write-Host "The Library $libraryName could not be found."

$files = ([System.IO.DirectoryInfo] (Get-Item $reportFilesLocation)).GetFiles()
foreach($file in $files)
	#Open file
	$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

	#Add file
	$folder =  $spSourceWeb.getfolder($libraryName)

	Write-Host "Copying file $file to $libraryName..."
	$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)

	#Close file stream

Write-Host "Files have been uploaded to $libraryName."

Download Script

And if you want to schedule, you can use Windows Scheduler to schedule your script.

Here are the steps to schedule a PowerShell script using Windows Task Scheduler.

  1. Save your PowerShell script to a location for e.g. C:\Manas\Scripts
  2. Go to Windows Task Scheduler and schedule your script using the command PowerShell.exe <PATH TO YOUR SCRIPT>.

There is this good post on using Scheduled Tasks to Run PowerShell Commands on Windows.

Published inUncategorized

Be First to Comment

Leave a Reply

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