Click here to Skip to main content
Click here to Skip to main content
Go to top

Upload Files to Library in SharePoint 2013 using PowerShell

, 20 Apr 2014
Rate this:
Please Sign up or sign in to vote.
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, 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 SharePoint environment with all these small tasks/timer jobs and have them maintained outside your SharePoint environment. In fact, it is not the 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.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.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."
    return;
}

$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
    $fileStream.Close();
}
$spSourceWeb.dispose();
Write-Host "Files have been uploaded to $libraryName."

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.

The post Upload Files to Library in SharePoint 2013 using PowerShell appeared first on Manas Bhardwaj's Stream.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Manas Bhardwaj
Architect
Netherlands Netherlands
Read my personal blog at www.manasbhardwaj.net.
Follow on   Twitter

Comments and Discussions

 
QuestionThe Library Shared Documents could not be found [modified] PinmemberBartEenkhoorn6-Jun-14 2:36 
AnswerRe: The Library Shared Documents could not be found PinprofessionalManas Bhardwaj6-Jun-14 23:20 
GeneralMy vote of 5 PinmemberAleksandar Pesic19-May-14 11:54 
QuestionAsset Library PinmemberAleksandar Pesic19-May-14 11:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 20 Apr 2014
Article Copyright 2014 by Manas Bhardwaj
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid