Using PowerShell to Bulk Upload Files to SharePoint

While building a school’s Learning Gateway I needed to bulk upload all their student images into a picture library so that the My Children web part could display them. Since there were several hundred of them I wrote a little PowerShell script to perform this.

$siteUrl = "http://sharepoint/schools/test"
$listName = "Students Picture Library"

[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.OpenWeb()
$list = $web.Lists[$listName]
$fileCollection = $list.RootFolder.Files

$files = get-childItem -Exclude *.ps1

foreach ($file in $files)
{
    $stream = $file.OpenRead()
    $uploaded = $fileCollection.Add($file.Name, $stream, $TRUE)
    "Uploaded " + $file.Name

    if ($stream) {$stream.Dispose()}
}

if ($web) {$web.Dispose()}
if ($site) {$site.Dispose()}

In order to use this:

  1. Save as as .ps1 file in the same folder as the files to upload.
  2. Change the site url and name of the list at the beginning of the script to your values
  3. Make sure that only the files to upload and the ps1 file is in the folder
  4. Open PowerShell using Run As Administrator
  5. Navigate to the folder
  6. Run the .ps1 file

The script will then iterate through all files in the current folder and upload them to the given list, overwriting them if they already exist.

Richard

I am Managing Director of SalamanderSoft Limited a company devoted to improving IT in education. I am also the project co-ordinator for the open source projects SharePoint Learning Kit and Microsoft Learning Gateway.

18 thoughts on “Using PowerShell to Bulk Upload Files to SharePoint

  1. This is just great! , thankyou soo much for uploading this Richard!

  2. Hi Richard,

    I am new to PowerShell, and SharePoint. What does “$ListName” refer to please? I have a folder on the SharePoint site called “Report Archives”. Should the ListName variable be the folder name? Thanks!

    Fang

  3. Hi Fang

    try putting a line break on line 6

    “$web = $site.OpenWeb()$list = $web.Lists[$listName]”

    do this

    $web = $site.OpenWeb()
    $list = $web.Lists[$listName]

  4. i think i didnt read carefully, list is referring to document library e.g. ‘shared documents’

  5. Hi Richard,
    Nice script though.
    But where are you moving the files from?
    Looking for a solution from a folder to a sharepoint lib.
    Cheers

  6. Hi Patrick,

    In this script it’s just from the folder the script is run from. To upload from elsewhere just change the
    $files = get-childItem -Exclude *.ps1
    line to get the list from another folder.

    Richard

  7. No as it’s using the SharePoint object model it needs to run on the SharePoint server. You could do something similar using the client side object model though.

  8. Will this script work if you want to upload files from a network share?

  9. I have a question about the opposite of this. How would I go about reading the items (pictures in my case as well) from this sharepoint list and save them out to one of my file servers? I validated that I am able to step into this script and read the contents of the sharepoint list specified, but instead of adding files to the list, what is the process of reading the files and saving them out to a share? Your help would be greatly appreciated. Thanks!

  10. If you swap out the foreach block with this it will save out the contents of the root folder

    foreach ($file in $fileCollection)
    {
    $localLocation = “outputFolder\” + $file.Name
    $sharePointStream = $file.OpenBinary()
    $stream = New-Object System.IO.FileStream($localLocation ,[System.IO.FileMode]::Create)
    $writer = New-Object System.IO.BinaryWriter($stream)
    $writer.write($sharePointStream)
    $writer.Close()
    $stream.Dispose()
    }
    If you have sub-folders you’ll have to iterate through them and do the same. I’ll leave that to you for practice.

  11. Hi,

    Thanks for the post. It is not bulk upload, as you are iterating though each file in the folder and adding into the document library. I also have the same scenario of uploading 1 million records from database to document library.
    Do we have anything bulk upload to perform this action?

    Thanks,
    Srikanth

  12. It’s a bulk uploader in that you only run one command to get all the files uploaded, but no it only transfers one file at a time.

    In you’ll case you’ll either need to find a 3rd party tool, do it in batches or write a more robust tool which logs any errors so you can retry.

  13. HI Richard, What if I want to read the file path [for file upload] from excel or notepad and then upload the file to SP2013
    -Sai

Leave a Reply

Your email address will not be published. Required fields are marked *