none
Upload document to folder in document library using powershell RRS feed

  • Question

  • Hi,

    How to upload multiple documents/images to a document library/Image library using powershell?

    In the library, I have a folder called Images. I want to upload the documents to that folder.

    I have checked this: http://get-spscripts.com/2010/07/upload-multiple-files-into-document.html

    I have used the below powershell:

    function UploadImages($weburl)
    {
    $docLibraryName = "TestLibrary"
    $localFolderPath = "c:\Install\Docs" 
    
    Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue 
    $web = Get-SPWeb -Identity $webUrl
    
    $docLibrary = $web.Lists[$docLibraryName] 
    #Attach to local folder and enumerate through all files
    $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object { 
    
        #Create file stream object from file
        $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
        $contents = new-object byte[] $fileStream.Length
        $fileStream.Read($contents, 0, [int]$fileStream.Length);
        $fileStream.Close(); 
    
        write-host "Copying" $_.Name "to" $docLibrary.Title "in" $web.Title "..." 
    
        #Add file
        $folder = $docLibrary.RootFolder
        $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
        $spItem = $spFile.Item 
    
    
    
    Write-Host -f Green "Added Images to ibrary !!!"
    }
    }

    Here the Images are added to the Root folder. How to add to the "Images" folder inside the document library.

    Thanks


    • Edited by Venkatzeus Friday, July 4, 2014 5:16 AM
    Friday, July 4, 2014 5:15 AM

Answers

  • Hi Venkat,

    Updated your script as below,please try

    Pass your folder name to the variable $subFolderName

    function UploadImages($weburl)
    {
    $docLibraryName = "TestLibrary"
    $localFolderPath = "c:\Install\Docs"
    $subFolderName="SubFolder" ##Pass your Folder Name Here
    
    Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue 
    $web = Get-SPWeb -Identity $webUrl
    
    $docLibrary = $web.Lists[$docLibraryName] 
    #Attach to local folder and enumerate through all files
    $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object { 
    
        #Create file stream object from file
        $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
        $contents = new-object byte[] $fileStream.Length
        $fileStream.Read($contents, 0, [int]$fileStream.Length);
        $fileStream.Close(); 
    
        write-host "Copying" $_.Name "to" $docLibrary.Title "in" $web.Title "..." 
    
        #Add file
        $folder = $web.getfolder($docLibrary.Title+"/"+$subFolderName) ##Changed code here to get subfolder
        $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
        $spItem = $spFile.Item 
    
    
    
    Write-Host -f Green "Added Images to library !!!"
    }
    }
    


    Please remember to click Mark as Answer on the answer if it helps you

    • Marked as answer by Venkatzeus Thursday, July 10, 2014 2:08 PM
    Friday, July 4, 2014 9:28 AM
  • Fixed it by using the below line:

     $folder = $web.getfolder($docLibrary.rootFolder.URL + "/" + $subFolderName)

    Thanks

    • Marked as answer by Venkatzeus Thursday, July 10, 2014 2:07 PM
    Thursday, July 10, 2014 2:07 PM

All replies

  • Hi,

    The question is different. I am not looking for creating a folder. Folder already exists in the library, and I want to upload the images to that folder.

    Thanks

    Friday, July 4, 2014 8:23 AM
  • Hi Venkat,

    Updated your script as below,please try

    Pass your folder name to the variable $subFolderName

    function UploadImages($weburl)
    {
    $docLibraryName = "TestLibrary"
    $localFolderPath = "c:\Install\Docs"
    $subFolderName="SubFolder" ##Pass your Folder Name Here
    
    Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue 
    $web = Get-SPWeb -Identity $webUrl
    
    $docLibrary = $web.Lists[$docLibraryName] 
    #Attach to local folder and enumerate through all files
    $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object { 
    
        #Create file stream object from file
        $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
        $contents = new-object byte[] $fileStream.Length
        $fileStream.Read($contents, 0, [int]$fileStream.Length);
        $fileStream.Close(); 
    
        write-host "Copying" $_.Name "to" $docLibrary.Title "in" $web.Title "..." 
    
        #Add file
        $folder = $web.getfolder($docLibrary.Title+"/"+$subFolderName) ##Changed code here to get subfolder
        $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
        $spItem = $spFile.Item 
    
    
    
    Write-Host -f Green "Added Images to library !!!"
    }
    }
    


    Please remember to click Mark as Answer on the answer if it helps you

    • Marked as answer by Venkatzeus Thursday, July 10, 2014 2:08 PM
    Friday, July 4, 2014 9:28 AM
  • HI,

    Thanks for the reply.

    I am getting the error as  "ForEach-Object : Exception calling "Add" with "3" argument(s):
    "<nativehr>0x80070003</nativehr><nativestack></nativestack>There is no file
    with URL 'http://sitename/TestLibrary/logo_image/tea_gif.gif' in this Web."

    I believe the error is somewhere in the below line:

       $folder = $web.getfolder($docLibrary.Title+"/"+$subFolderName) 
        $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
        $spItem = $spFile.Item 

    Because, I can see the write host value before that line.

    How to fix this?

    Thanks


    • Edited by Venkatzeus Monday, July 7, 2014 8:10 AM
    Friday, July 4, 2014 11:43 AM
  • Fixed it by using the below line:

     $folder = $web.getfolder($docLibrary.rootFolder.URL + "/" + $subFolderName)

    Thanks

    • Marked as answer by Venkatzeus Thursday, July 10, 2014 2:07 PM
    Thursday, July 10, 2014 2:07 PM