Click here to Skip to main content
15,881,455 members
Articles / Productivity Apps and Services / Sharepoint

Upload Files to Library in SharePoint 2013 using PowerShell

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
20 Apr 2014CPOL2 min read 61.2K   4   7
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)


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
QuestionScript will not work for SharePoint onlie Pin
Vivek Verma SP1-Oct-15 8:12
Vivek Verma SP1-Oct-15 8:12 
QuestionRemote script running Pin
Member 103807007-May-15 1:41
Member 103807007-May-15 1:41 
QuestionCan we calculate the time while uploading? Pin
osamabg1-Apr-15 10:58
osamabg1-Apr-15 10:58 
QuestionThe Library Shared Documents could not be found Pin
BartEenkhoorn6-Jun-14 2:36
BartEenkhoorn6-Jun-14 2:36 
AnswerRe: The Library Shared Documents could not be found Pin
Manas Bhardwaj6-Jun-14 23:20
professionalManas Bhardwaj6-Jun-14 23:20 
GeneralMy vote of 5 Pin
Aleksandar Pesic19-May-14 11:54
Aleksandar Pesic19-May-14 11:54 
QuestionAsset Library Pin
Aleksandar Pesic19-May-14 11:49
Aleksandar Pesic19-May-14 11:49 

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

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