Auto upload file share documents to SharePoint

Cherry P 21 Reputation points
2023-09-07T14:02:09.5733333+00:00

Hi,

We are using SharePoint server 2019 on-premise. Is there a way to automatically upload files from local drive to SharePoint in a daily basis? I believe we can achieve this with Powershell script and scheduling a task in the Task scheduler.

Can I have more details on this with detailed script and the steps? appreciate your help.

Thanks

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint Server | Development
Microsoft 365 and Office | PowerPoint | For business | Windows
Windows for business | Windows Server | User experience | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,601 Reputation points Microsoft External Staff
    2023-09-08T08:52:59.27+00:00

    Hi @Cherry P ,

    According to your description, task scheduler is a Windows-related app, not my field of expertise, so I added a Windows-related tag. Regarding your question, from the perspective of SharePoint, it is completely feasible to upload files from local drive to SharePoint through PowerShell. I can provide the code for you to try, but how to combine it with the Task Scheduler to achieve automatic upload requires a reply from a Windows expert.

    Here is the code:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
      
    #Function to Upload File
    Function Upload-AllFilesFromDir($WebURL, $DocLibName, $FolderPath)
    {
        #Get the Web and List to upload the file
        $web = Get-SPWeb $WebURL
           
        #Get the Target Document Library to upload
        $List = $Web.GetFolder($DocLibName)
      
        #Get the Files from Local Folder
        $Files = Get-ChildItem $FolderPath #You can filter files by: -filter “*.pdf”
     
        #upload the files
        Foreach ($File in $Files)
        {
            #Get the Contents of the file to FileStream
            $stream = (Get-Item $file.FullName).OpenRead()
         
            # Set Metadata Hashtable For the file - OPTIONAL
            $Metadata = @{"Country" = "United States"; "Domain" = "Sales"}
         
            #upload the file             
            $uploaded = $List.Files.Add($File.Name, $stream, $Metadata, $TRUE)
         
            #dispose FileStream Object
            $stream.Dispose()
        }
    }
       
    #call the upload function
    Upload-AllFilesFromDir "https://SharePoint.crescent.com/sites/sitename/" "Shared Documents" "c:\Documents\"
    

    For more details, please refer to:

    https://www.sharepointdiary.com/2012/10/bulk-upload-files-to-sharepoint-using-powershell.html

    And here is a similar post for your reference:

    https://sharepoint.stackexchange.com/questions/208425/powershell-script-to-transfer-files-from-local-folder-to-document-library

    Hope this helps.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.