PowerShell: Bulk Upload Files to SharePoint remotely using CSOM

This is a parameterized script that will perform bulk uploads of files from your local file system to SharePoint remotely.  This script uses CSOM and will maintain your existing file and folder structure, as it exists on your local file system.Usage is relatively simple: .\BulkUp

 
 
 
 
 
4.3 Star
(15)
10,064 times
Add to favorites
Office 365
2/28/2015
E-mail Twitter del.icio.us Digg Facebook
  • How to upload changed files only
    1 Posts | Last post January 04, 2020
    • This script works as expected, thank you.  I'm trying to modify it to upload only files that have been changed in my directory structure, but failing miserably.  Can you tell me if this is even possible?
      
      Thank you
  • How to bind to site when using 2 Factor Auth
    1 Posts | Last post July 11, 2019
    • This script fails for me letting me know "The Sign-in name or password does not match one in the Microsoft account System"  As I understand it the issue is our use of 2 Factor Authentication which I cannot bypass.
      
      I am certain of my credentials since I am able to login to the 0365 Admin Portal.  I am attempting to import files to a document library in SharePoint Online.
      
      Ideally I think I want to use an AppID and AppSecret so this won't rely on my user ID.  Can you provide any guidance on how to do that?
      
      Thank you for sharing your code.
      
      
  • Exception calling "ExecuteQuery" with "0" argument(s):
    1 Posts | Last post January 08, 2019
    • I'm getting this error:
      
      Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
      At D:\Scripts\BulkUploadSharePointCSOM\BulkUploadSharePointCSOM.ps1:202 char:1
      + $Context.ExecuteQuery()
      + ~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : WebException
       
      Uploading file  01-203.docx to SSOs_Test
      New-Object : Exception calling ".ctor" with "2" argument(s): "The process cannot access the file 'D:\DownloadFiles\SSOs\01-203.docx' because it is being used by another process."
      At D:\Scripts\BulkUploadSharePointCSOM\BulkUploadSharePointCSOM.ps1:64 char:19
      + ... ileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMo ...
      +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
          + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
  • Script creating full absolute path
    2 Posts | Last post December 12, 2018
    • Try as I might I can't get the script to stop using the full absolute path when creating the Sharepoint structure.
      
      For example, if I have c:\Users\JD\Desktop\Stuff\<subfolders> and I'm running from \Stuff I'm expecting the site to populate with Stuffs contents and subfolders.  Instead it creates c:\Users\JD\Desktop\Stuff\<subfolders> and populates from C: up
      
      Just wondering if you had any suggestions.
      
      Thanks a ton for all your work btw.
    • Nm, fixed it by trimming the paths.
  • How to use as part of a scheduled task
    1 Posts | Last post July 17, 2018
    • Hi All,
      
      We are trying to utilise this as part of a wider script which exports the membership of an Active Directory group to a .csv
      
      We want to do this via a Task Schedule but when we try to run it automatically it fails with an access denied message however when we run the script manually (using the same account) via ISE or straight Powershell prompt it works perfectly.
      
      Any ideas why it wouldn't work via Scheduled Task? We weren't sure if it has something to do with the password being stored in the script.
      
      Here is our script (I have anonyomised the data):
      
      import-module activedirectory
      
      Get-ADGroup -Server "servername" -Filter * -SearchBase “OU=Test,DC=companyname,DC=com” -Properties Name, Description, whenChanged | Select Name, Description, whenChanged | Sort-Object -Property Name | export-CSV "J:\Export\export.csv" -NoTypeInformation
      
      .\BulkUploadSharePointCSOM.ps1 -UserName "username@contoso.onmicrosoft.com" -Password "password" -SiteURL "https://contoso.sharepoint.com/sites/TestSite/" -DocLibName "Documents" -Folder "J:\Export\" -O365 $True
  • Roger, do you have version which include proxy handlig?
    2 Posts | Last post March 14, 2018
    • Great job Roger.
      
      Do you have a version of the script which works with proxy?
    • I haven't tested with proxy - is it an authenticated proxy?  It would be possible to just add some logic at the beginning of the script to use the default proxy settings from the browser, I've had to do that for other scripts that connect to other services (like exchange online)
  • Getting the following error
    4 Posts | Last post July 19, 2017
    • Hi there , im trying to use the script but getting the following error
      
      Uploading file  test.pptx to Documents
      Exception calling "ExecuteQuery" with "0" argument(s): "List 'Documents' does not exist at site with URL
      'https://mysite.sharepoint.com'."
      At C:\Users\admin\Desktop\BulkUploadSharePointCSOM.ps1:58 char:9
      +         $Context.ExecuteQuery()
      +         ~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : ServerException
      
      Exception calling "CheckIn" with "2" argument(s): "The object is not associated with an object identity or the object
      identity is invalid."
      At C:\Users\admin\Desktop\BulkUploadSharePointCSOM.ps1:61 char:13
      +             $Upload.CheckIn("Checked in by Administrator", [Microsoft ...
      +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : ClientRequestException
      
      Exception calling "Load" with "1" argument(s): "The object is not associated with an object identity or the object
      identity is invalid."
      At C:\Users\admin\Desktop\BulkUploadSharePointCSOM.ps1:64 char:5
      +     $Context.Load($Upload)
      +     ~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : ClientRequestException
    • Unfortunately also getting the same message about "does not exist at site with URL"
      
      Also, wondering why I'm getting a CheckIn exception call when I don't use that switch when running the script.
      
      Exception calling "CheckIn" with "2" argument(s): "The object is not associated with an object identity or the object identity is invalid."
      
      .\BulkUploadSharePointCSOM.ps1  -O365 $True -UserName "myusername@mydomain" -Password "MyPass" -SiteURL "https://mysite.sharepoint.com" -DocLibName "My Shared Documents Location" -Folder "C:\Users\MyName\TestSPupload"
    • getting the same :(
      
      
      Exception calling "ExecuteQuery" with "0" argument(s): "The remote server retur
      ned an error: (429)."
      At C:\Script\BulkUploadSharePointCSOM.ps1:58 char:9
      +         $Context.ExecuteQuery()
      +         ~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : WebException
      
      Exception calling "CheckIn" with "2" argument(s): "The object is not associated
       with an object identity or the object identity is invalid."
      At C:\Script\BulkUploadSharePointCSOM.ps1:61 char:13
      +             $Upload.CheckIn("Checked in by Administrator", [Microsoft.SharePo
      int ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : ClientRequestException
      
      Exception calling "Load" with "1" argument(s): "The object is not associated wi
      th an object identity or the object identity is invalid."
      At C:\Script\BulkUploadSharePointCSOM.ps1:64 char:5
      +     $Context.Load($Upload)
      +     ~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : ClientRequestException
      
      Exception calling "ExecuteQuery" with "0" argument(s): "The remote server retur
      ned an error: (429)."
      At C:\Script\BulkUploadSharePointCSOM.ps1:65 char:5
      +     $Context.ExecuteQuery()
      +     ~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : WebException
    • I had the same problem.  It turned out that I was looking at the whole thing the wrong way.  SharePoint doesn't use the same filesystem structure as Windows.  You need to run an inventory script on your site to determine what's a website, library or folder.  Once you have that, you can figure out where to break the destination URL into $SiteURL and $DocLibName.  It's not as obvious as you would think.
  • SubFolder
    2 Posts | Last post May 10, 2017
    • Hi,
      
      The code works fine, but I need to upload the files to sub-folder "Documents\Test" on SharePoint Online.
      
      any help would be greatly appreciated
      
      Thanks
    • Maybe create a local folder with the sub-folder name that holds your upload files then pass its parent folder path to -Folder. I was able to load to a sub-folder on Documents this way.
  • Can we preserve CreatedBy in the script.
    1 Posts | Last post March 30, 2017
    • Has anyone added metadata to the script?  I'm able to set the title, even change Created (date) but CreatedBy is elusive.  For SPO the user string must be ID;#email e.g. 9;#pespo@bluedogtec.com.  I believe this is correct since any other format tosses an exception... but its not changing.
      
      After getting context and uploading the file...
      
        $item = $upload.ListItemAllFields  # get the item from the just uploaded file 
        $date = get-date  # setting some values
        $item["Title"]   = "File: {0}   Time: {1}" -f $file.name.ToString(), $date  # this works
        $date = (get-date).AddDays(-100) # something that changes but its not now
        $item['Created'] = $date  # this works
      
        $item['Author']  = "9;#pespo@bluedogtec.com"        # this doesn't work,
        $item.Update()     # Title and Created are updated ...
        $ctx.ExecuteQuery()
  • Excel files
    2 Posts | Last post February 02, 2017
    • Hi,
      
      I got this working with text files but am unable to load Excel files.  Is there a binary version?  thanks
    • When I initially wrote this script, I had tested with a few GB of random files.  There were all types of office files, PowerShell scripts, images, videos, and did not run into any errors with specific file types.  What kind of error are you encountering?
1 - 10 of 31 Items