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


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

#Dispose web


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


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


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


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!


    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:


    1. Hi Rich,

      Have a look at this article which shows a simple method to set properties on uploading a file:

      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:

      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.


  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)


    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

      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


  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.


  4. Hi Cameron,

    In our company a daily zip file gets regularly uploaded in sharepoint((For ex – ,next day it would be

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



    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)
      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.


  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.


  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?


    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

Create a website or blog at

Up ↑

%d bloggers like this: