How to import (upload) an entire folder of files to SharePoint using PowerShell

SharePoint PowerShell How To Series – Foreword

I often find myself scratching around the local drive of my development machines for remnants of PowerShell scripts to generate list & libraries in SharePoint with all sorts of different folder hierarchies to test the performance of code I’m working on to ensure it scales well once the folder & item counts start getting up to high numbers. So rather than keep scratching I’m going to start posting my scripts as I create and use them so I can find/reuse them later on and someone else might find them useful as well. I’m sure they won’t work under all scenarios and situations (and they are not designed to). They are just quick scripts that serve a specific purpose to me at the time and you may be able to tweak to fit your needs.

Note this example doesn’t handle folders within folders. There are also several “no-code” methods for uploading a whole folder structure (and files) to SharePoint.

1. Create a folder containing the files to upload to SharePoint

files-to-import-upload-to-sharepoint-cameron-dwyer

2. Locate the SharePoint library that you want to upload the files to, take note of the:

  • Web URL
  • Document library URL
  • Document library name

3. Copy the following script to be run in a PowerShell window

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Script settings

$webUrl = "http://vs-server12"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared%20Documents"

$localFolderPath = "C:\temp\Docs4SharePoint"

#Open web and library

$web = Get-SPWeb $webUrl

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()

ForEach($file in $files)
{

    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)

    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"

    #Close file stream
    $fileStream.Close();
}

#Dispose web

$web.Dispose()

4. Change the PowerShell variables at the top of the script to match your SharePoint upload destination and the location of the folder of files to upload

powershell-variable-to-change-sharepoint-cameron-dwyer

5. Run the PowerShell script, you should see progress of the uploaded files as shown below

result-powershell-save-files-to-sharepoint

6. After the script completes you should see all of the imported files in your SharePoint document library

result-entire-folder-files-imported-to-sharepoint-cameron-dwyer

Related articles

30 thoughts on “How to import (upload) an entire folder of files to SharePoint using PowerShell

Add yours

  1. Very helpful Cameron, I have to regularly uploads recorded training sessions as collateral for our end user support. This is a great way to expedite the process, so again, many thanks!

    Like

    1. Hi Robert,

      The PowerShell cmdlets for SharePoint Online is very limited compared to the full SharePoint 2013 (or 2010) PowerShell cmdlets. It’s mainly limited to SHarePoint Administration tasks and not SharePoint content.

      You can reference the SharePoint Client Side Object Model (CSOM) dll from PowerShell scripts to achieve what you are after – but you’re then really writing CSOM code (which is much nice doing from Visual Studio in a C# project). I haven’t done this from PowerShell directly (as I would do it straight from a C# project) but it should work fine. For examples of this technique please take a look at these links:

      http://jeffreypaarhuis.com/2012/06/07/scripting-sharepoint-online-with-powershell-using-client-object-model/

      http://www.sharepointnutsandbolts.com/2013/12/SharePoint-Online-and-MSOL-WAAD-PowerShell-Office365.html

      Like

    1. Hi Rich,

      Have a look at this article which shows a simple method to set properties on uploading a file:
      http://social.technet.microsoft.com/Forums/office/en-US/ad6555c8-51ba-4c85-adcd-e2114fed2a4c/importing-metadata-to-document-library-columns

      The above is a simplified example, it should work on text columns but other column types (e.g. Dates, Managed Metadata, Lookup, Hyperlink, Picture) will probably need to set set differently. If you are in this situation then maybe take a look at this article:
      http://get-spscripts.com/2010/10/bulk-upload-files-with-metadata-into.html

      Your version of SharePoint will also make a difference when you get into some of the column types, for example Managed Metadata columns are easier to set with SharePoint 2013 than it is in 2010.

      Like

  2. Thanks for the info. Just remember that other tools exist as well. For example, you can use robocopy to mirror an entire file structure with just one command (and it handles differential changes easily)

    Like

    1. Hi Alex,
      It’s hard to provide assistance in your scenario since it’s doing nothing. I’d suggest taking a look at this article to see if it gives you any ideas as to what might be failing
      https://camerondwyer.wordpress.com/2014/06/18/getting-started-the-basics-of-using-powershell-with-sharepoint/

      In your case the docLibraryUrlName should be the segment of the URL that is the document library. Your full URL contains the site collection (plus any subsites), then the library (which is the bit you need to assign to the variable, then the URL possibly also includes folders within the library and finally the Forms/AllItems.aspx is just static reference to show the view from the library. From looking at the URL I can’t tell which bit is site and which bit is library and which bit is folder structure within the library. If I had to take a guess I’d say try https://sitename/funworld/psupp/gd/maga as your site, then your docLibraryUrlName should be “Upload” or maybe your library is maga and you created a folder within it called Upload

      Like

  3. Hi Cameron,
    nice script. Works pretty well for me.
    I tried to re-write to allow me uploading files from a folder given the filenames and Metadata from a CSV file. But my attempts failed. Do you have a solution here as well? I’m focused on Metadata edited within Excel and then uploaded to a SharePoint library.

    Like

  4. Hi Cameron,

    In our company a daily zip file gets regularly uploaded in sharepoint((For ex – 29-jan-2016-filename.zip) ,next day it would be 20-jan-2016-filename.zip.)

    Is it possible to write powershell program that automatically downloads the daily file based on date to the loca machine ?

    Thanks
    Vishnu

    Like

    1. Hi Vishnu. This is certainly possible using Powershell. I don’t have any scripts handy that do exactly what you are after, but take a look at this article for how to download a file from SharePoint (it provides a few different ways) http://techtrainingnotes.blogspot.com.au/2014/02/download-file-from-sharepoint-using.html
      And this MSDN article provides lots of info on how to get the current date/time and format it in different ways in Powershell. Combine the two and you should be able to accomplish your goal.
      https://technet.microsoft.com/en-us/library/ee692801.aspx

      Like

  5. Hi Cameron, I’m trying to export a pdf from Tableau Server and import it directly to One Drive, i don’t want to store anything on local, any suggestion. Thanks Nish.

    Like

  6. I tried this script many times, this does not work and gives below error:

    Exception calling “Add” with “3” argument(s): “0x80070003There is no file with URL

    I think, if you have subfolders structure where you want to move the data below gives above error:

    #Add file
    $folder = $web.getfolder($docLibraryUrlName)

    Do you have anything on this?

    Thanks,
    DC

    Liked by 1 person

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑