共用方式為


Office 365 - PowerShell Script to Upload Files to a Document Library using CSOM

UPDATE: My colleague Roger Cormier has created a far superior version of this script that is able to handle sub-folders, please check it out - https://gallery.technet.microsoft.com/PowerShell-Bulk-Upload-b9e9d600

Another PowerShell sample script for you. This one uploads all files within a specified local directory to a Document Library within a Site in an O365 tenant.

All you need to run this script is an O365 tenant, the SharePoint client components SDK installed on the machine running the script - https://www.microsoft.com/en-us/download/details.aspx?id=35585 and to update the $User, $SiteURL, $DocLibName (name of the destination Document library) and $Folder (path to the local folder containing the files to upload) variables. When the script is executed it will prompt for the password of the user specific in the $User variable.

#Specify tenant admin and site URL
$User = "admin@tenant.onmicrosoft.com"
$SiteURL = "https://tenant.sharepoint.com/sites/site"
$Folder = "C:\FilesToUpload"
$DocLibName = "DocLib"

#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

#Upload file
Foreach ($File in (dir $Folder -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}

Brendan Griffin

Comments

  • Anonymous
    January 01, 2003
    Looks as though it is having an issue connecting to the list specified in the $DocLibName variable, may be worth checking this to ensure that the library is present.
  • Anonymous
    January 01, 2003
    Yes, I'm actually working on something very similar at the moment. Will post once it's ready.
  • Anonymous
    January 01, 2003
    I have updated the script to support files larger than 2MB.
    • Anonymous
      January 26, 2017
      Hi Brendan,Is the script updated? Still seems to only support 2mb files.
  • Anonymous
    January 01, 2003
    There is an article here - http://www.c-sharpcorner.com/Blogs/12139/how-to-create-document-set-using-csom-in-sharepoint-2013.aspx but it's using C#, should be easy enough to convert to PS. I may even do a Blog post on it :)
  • Anonymous
    January 01, 2003
    Office 365 - PowerShell Script to Upload Files to a Document Library using CSOM
    hassan sayed issa
  • Anonymous
    February 20, 2014
    Thanks Brendan - do you know of a similar routine that would let one create a new Document set and upload documents into it?
  • Anonymous
    February 21, 2014
    Great, thanks. One more question- in regards to the 2MB limit- the Answer on the post at http://social.msdn.microsoft.com/Forums/sharepoint/en-US/deac7cb7-c677-47b0-acdc-c56b32dfaac8/uploading-bigger-files-using-csom?forum=sharepointdevelopment seems to demonstrate the streaming method necessary to overcome the 2MB limit. Do you know if that's possible to recreate the streaming method in PowerShell as opposed to the C# example provided?
  • Anonymous
    March 13, 2014
    Hi, brilliant work, do you know if its possible to create a script to download/ copy all documents from an O365 site collection to a local drive?

    Thanks
  • Anonymous
    March 23, 2014
    Love the script.
    Wondering how to tweak it to upload meta data with the files also?
    Thanks.
  • Anonymous
    May 02, 2014
    Hi, is it possible to share the similar script for download?
  • Anonymous
    May 04, 2014
    I am not sure why I am only getting the below error while executing this code. Please help me to fix.

    You cannot call a method on a null-valued expression.
    At D:SP2013 OnlineOnlineWorksget site informationtest.ps1:24 char:1
    + $List = $Context.Web.Lists.GetByTitle($DocLibName)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At D:SP2013 OnlineOnlineWorksget site informationtest.ps1:25 char:1
    + $Context.Load($List)
    + ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At D:SP2013 OnlineOnlineWorksget site informationtest.ps1:26 char:1
    + $Context.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    2
    True
    3
    True
    2
    Success
    Close
    You cannot call a method on a null-valued expression.
    At D:SP2013 OnlineOnlineWorksget site informationtest.ps1:63 char:1
    + $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At D:SP2013 OnlineOnlineWorksget site informationtest.ps1:64 char:1
    + $Context.Load($Upload)
    + ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At D:SP2013 OnlineOnlineWorksget site informationtest.ps1:65 char:1
    + $Context.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
  • Anonymous
    May 08, 2014
    Trying to move a file inside sharepoint from folder A to folder ADone (After processing). Do you have any sample script i can use to move a file?

    Thanks
  • Anonymous
    May 12, 2014
    The comment has been removed
  • Anonymous
    July 01, 2014
    Hi, Can you please provide the script to upload more than 2MB file size, I am looking up to 2GB file size upload. Please send script to sharepointind@gmail.com

    Thanks for your help.
  • Anonymous
    July 22, 2014
    Hi,
    I was wondering if you are able to create folders in the same way?
  • Anonymous
    October 10, 2014
    I hope you would add some lines to the script to keep the changed date and the author of the files.
  • Anonymous
    January 28, 2015
    Is there a way to have the file checked in? As when it gets uploaded it is left checked out...would like to automate the check in process as well.

    Thanks.
  • Anonymous
    February 10, 2015
    Is it possible to have the results of what is going on in the script be displayed on the screen for when I have users run the script?
  • Anonymous
    February 12, 2015
    I am also looking for Powershell Script for Creating Document Sets in Bulk.
    Please give some idea or any body can send some script for this.
    I am using SharePoint online 2013(Office 365)
  • Anonymous
    February 17, 2015
    Your script works quite well, but I'm having a devil of a time figuring out how to upload to an established subdirectory/subfolder in the library. Any advice would be helpful. Thanks!
    • Anonymous
      December 14, 2016
      Did you ever find a way to upload files and folders to a doc Library?Thx,Sam
  • Anonymous
    May 04, 2015
    How about script to upload a list template file in all user's mySite?
    Thanks.
  • Anonymous
    June 01, 2015
    Is there a way of retaining the office documents properties
  • Anonymous
    August 12, 2015
    Hi brandon i need powershell script to download files from o365 on specified local directory.
    plz need that script , thanks in advance
  • Anonymous
    September 18, 2015
    I managed to get this Script working on SharePoint 2013 site, I would like to upload the file(s) into a subfolder in the site Library, but have not been able to achieve this, how can this be done?

    #Specify tenant admin and site URL
    $user = "Username@domain.com"
    $SiteURL = "http://mysite2013.test.domain.com/personal/spruit1/Shared_Library/"
    $Folder = "C:TempTemp1"
    $DocLibName = "Shared_Library"

    #Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
    Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
    Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
    $Password = Read-Host -Prompt "Please enter your password" -AsSecureString

    #Bind to site collection
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,$Password)
    $Context.Credentials = $Creds
    # !!Had s removed from above $creds!!

    #Retrieve list
    # $List = $Context.Web.Lists.GetByTitle($DocLibName)
    $List = $Context.Web.Lists.GetByTitle("Certificates")
    $Context.Load($List)
    $Context.ExecuteQuery()

    #Upload file
    Foreach ($File in (dir $Folder -File))
    {
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    $Context.Load($Upload)
    $Context.ExecuteQuery()
    }
    # source: http://blogs.technet.com/b/fromthefield/archive/2014/02/19/office365-script-to-upload-files-to-a-document-library-using-csom.aspx
    ___________________________
    I tried to use $DocLibName = "Shared_Library/SubFolder" and created this folder first.

    I also managing SharePoint 2010 sites and like to achieve the same, however above script which work for SharePoint 2013 sites fails for SharePoint 2010 sites.

    How can I achieve this?
    Regards Petro.
  • Anonymous
    September 18, 2015
    @Petro - here is an updated version that can target a specific folder:

    $FolderName is the name of the folder within the doc lib to upload files to.

    $Folder = "D:FilesToUpload"
    $DocLibName = "Documents"
    $FolderName = "Folder"

    #Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
    Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
    Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
    $Password = Read-Host -Prompt "Please enter your password" -AsSecureString

    #Bind to site collection
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
    $Context.Credentials = $Creds

    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocLibName)
    $Context.Load($List)
    $Context.ExecuteQuery()

    #Retrieve folder
    $FolderToBindTo = $List.RootFolder.Folders
    $Context.Load($FolderToBindTo)
    $Context.ExecuteQuery()
    $FolderToUpload = $FolderToBindTo | Where {$_.Name -eq $FolderName}

    #Upload file
    Foreach ($File in (dir $Folder -File))
    {
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $Upload = $FolderToUpload.Files.Add($FileCreationInfo)
    $Context.Load($Upload)
    $Context.ExecuteQuery()
    }
  • Anonymous
    September 21, 2015
    Thanks for sharing this informative article.
    Though, in my case to fulfill our requirement without technical expertise, we used a tool named LepideMigrator for Documents (http://www.lepide.com/lepidemigratordocuments/ ) that worked perfectly to get this job done.
    However, I have bookmarked this article to get help in coming future and hope, it would be a nice destination for us.
  • Anonymous
    September 29, 2015
    The comment has been removed
    • Anonymous
      May 18, 2016
      The comment has been removed
      • Anonymous
        July 22, 2016
        I'm having the same issue in Sp2010, did you solve it?Help wuold be very much appreciated.Thanks
        • Anonymous
          May 05, 2017
          Old thread but someone could bump into it like I did. You should use $FileCreationInfo.Content instead of $FileCreationInfo.ContentStream and pass it a byte array. My case is different since I copy from one library to another, so I don't know if the conversion to byte array should look like this for you. This is the way I do it:[byte[]]$fileCreationContent = $FileStream.ToArray()$FileCreationInfo.Content = $FileStreamGreetings,George
          • Anonymous
            May 05, 2017
            Oops,[byte[]]$fileCreationContent = $FileStream.ToArray()$FileCreationInfo.Content = $fileCreationContent
  • Anonymous
    January 22, 2016
    Excellent, very easy to use. Please could you also explain how I can also add field/column data to the file that's uploaded. Once the file is online, I could use $item["Project Name "] = "Test Project" but can this be done within the upload script?
  • Anonymous
    January 26, 2016
    $item = $Upload.ListItemAllFields
    $item["Customer Field"] = "Field Text"
    $item.Update()
  • Anonymous
    February 03, 2016
    The Folder script in the comments worked to get the files into a folder off root.
    How can I upload the file into a folder 2 deep? A subfolder of a folder off root?
    Thanks.
  • Anonymous
    December 14, 2016
    Looking to go one step further and copy an entire folder structure (folder with sub-folders and files) to another folder in a document Library of another site.Man task is:Create a folder in the document library, then copy a default local folder/files to the newly created folder on the sub-site.Any help would be appreciated,Sam
  • Anonymous
    December 23, 2016
    The comment has been removed
  • Anonymous
    March 29, 2017
    Hello Brendan,I am currently using parts of your script to copy Files from a SharePoint 2013 to Office 365 and I am facing following issue:I try to set$fileStream=New-Object IO.FileStream($fileFullName, [System.IO.FileMode]::Open)Where $fileFullName looks something like this:\ourSharePoint@SSL@Port\DavWWWRoot\Distribution\Employee\File.xlsxI had this working already and successfully copied files from our on premise system to SharePoint Online.I didn't change a thing and suddenly the function ends up in following error:Exception calling ".ctor" with "2" argument(s): "Could not find Network Path."If I write the $fileFullname via: write-host $fileFullName - and then copy the output and paste it to the Windows Explorer, I don't have any trobules. It opens the document library in explorer view with all the files in it.It just fails in the Script.I would greatly appreciate help with this issue!
  • Anonymous
    May 26, 2017
    Hello Brendan,Great script but having trouble moving it to SharePoint Online (2016) in PowerShell since the new version of PowerShell takes the Pnp cmdlets. The files will upload in a folder but the subfolders and files in them do not get uploaded. I get a bunch of errors regarding the ProcessDropOffLibraryItem not being a recognized as the name of a cmdlet, function, script file, or operable program. Do you know of a version of uploading folders & files to SharePoint Online using PnP cmdlet's?Thanks,John