Home > PowerShell, SharePoint > Using PowerShell to Bulk Upload Files to SharePoint

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.

  1. Shahid Mahmood
    July 8th, 2011 at 21:38 | #1

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

  2. Fang
    September 26th, 2011 at 23:12 | #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. Rizwan
    April 13th, 2012 at 11:26 | #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. Rizwan
    April 13th, 2012 at 11:27 | #4

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

  5. Patrick
    December 4th, 2012 at 07:26 | #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. December 21st, 2012 at 22:04 | #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. Shoaib
    April 29th, 2013 at 09:37 | #7

    Hi, Will this work on Sharepoint Online??

  8. April 29th, 2013 at 09:46 | #8

    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.

  1. July 6th, 2011 at 20:37 | #1