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

About Cameron Dwyer

Chief Technology Officer at OnePlace Solutions. Microsoft MVP: Office Dev Passionate about delivering compelling solutions on the Office 365/SharePoint platform. Addicted to coffee.

Posted on May 27, 2013, in Office 365, PowerShell, SharePoint and tagged , , , . Bookmark the permalink. 28 Comments.

  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!


  2. Use Long Path Tool for such problems, it works good…


  3. Thanks so much. It helps me a lot.
    Can I use the powershell script create folder? If I want to upload many folder and files.


  4. Cameron, I have just a quick question. Can you have your script to generate documents from a single document and then mass upload them to SharePoint Online? Like in your post here:


  5. Hi Cameron,

    Do you also have code examples for setting document properties during the copy process?



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


  7. Hi Cameron

    Quite straithforward article!

    Many thanks for the share:)

    Thanks again.


  8. Robert Vargas

    Thanks Cameron! This helped me a great deal!


  9. good day, it’s useful script, thank you, but in my case it’s not working, could you please help? nothing happaned. and also could you explain about this field
    $docLibraryUrlName = “Upload” ? In my case it looks like this:


    • 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


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


  11. 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 ?



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


  13. Is there a way to run this script from Windows 10 PC?


  14. Hi Cameron,
    I have a opposite case. Can you provide something that copies all folders and files from SharePoint document library to file share maintaining folder/file structure with metadata? I am using
    It used to work but now all of a sudden it stopped working. It only deletes the folders but does not copy anything.


  1. Pingback: How to bulk upload/copy a folder structure and files to SharePoint | Cameron Dwyer | SharePoint, Outlook, OnePlaceMail

  2. Pingback: How to bulk upload/copy a folder structure and files to SharePoint | SPOmani

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 )

Google photo

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

Twitter picture

You are commenting using your Twitter 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.

%d bloggers like this: