Monday, October 1, 2012

Bulk Upload Files to SharePoint using PowerShell

Requirement is to upload multiple files from a local folder to SharePoint document library along with its Metadata.

Bulk upload can be done via SharePoint interface by selecting multiple file upload, however setting metadata may be an issue, when it comes to large number of documents.

PowerShell script to Bulk Upload Files to SharePoint Library:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Function to Upload File
function UploadAllFilesFromDir($WebURL, $DocLibName, $FolderPath)
{
 #Get the Web & Lists to upload the file
 $site = New-Object Microsoft.SharePoint.SPSite($WebURL)
 $web= $site.OpenWeb()
      
#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()
}
 
#Dispose the site object
$site.Dispose()
}
  
#call the upload function
UploadAllFilesFromDir "http://SharePoint.crescent.com/sites/sales/" "Shared Documents" "c:\Documents\"

Upload Files from CSV and apply Metadata in Bulk: 
Here is my CSV file with list of file names and metadata.

PowerShell to upload and apply metadata in bulk:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#Configuration Variables
$SiteURL="https://sharepoint.crescent.com/sites/sales/"
$LibraryName="Team Docs"
$FilesPath="d:\FilesToUpload\"
$CSVFilePath="d:\UploadData.csv"

#Get the Web
$Web = Get-SPWeb $SiteURL
#Get the Target Document Library to upload
$Library = $Web.GetFolder($LibraryName)

#Get CSV file contents
$CSVData = Import-CSV -path $CSVFilePath

#Iterate through each Row in the CSV
Foreach ($Row in $CSVData)
{
    write-host "Uploading File:"$Row.FileName

    #Get the File from local disk 
    $SourcePath= Join-Path $FilesPath -ChildPath $Row.FileName
    $SourceFile = (Get-ChildItem $SourcePath).OpenRead()

    #Set Metadata Hashtable For the file
    $Metadata = @{"Country" = $Row.Country; "Department" = $Row.Department}

    #upload the file              
    $uploaded = $Library.Files.Add($Row.FileName, $SourceFile, $Metadata, $TRUE)
}




You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

23 comments :

  1. I have read ur blogs on SharePoint in past, you are amazing to gain some knowledge to SharePoint. good knowledge to SharePoint and have discussed the things in details to which one can easily understand the things.

    ReplyDelete
  2. Thank you so much for the post this is very helpful!

    One question, does -filter exclude everything that's a pdf in this scenario? what if i want it to only get the files that have a pdf file extension?

    ReplyDelete
    Replies
    1. Hi Mechachai, -Filter will get you only the filetypes specified. E.g. Get-ChildItem -filter "*.pdf" will retrieve all PDF Files.

      If you want to get all files except PDF, You can use "-Exclude" switch. E.g. Get-ChildItem -exclude "*.pdf"

      Delete
  3. Hi,
    I carried out the above and realised it is not working.
    Step for step all are the same with yours. I even did the following
    -added some line of codes to check if the files were being read from the destination and that was a YES.
    -added another line to fine out if the files were being written to the destination just after the code snippet below
    #upload the file
    $uploaded = $List.Files.Add($File.Name, $stream,$Metadata, $TRUE)
    "$File written to destination" | Out-File $ReportFile -Append
    the result came out that the files were written to the destination successfully.

    However when I check the Document library via the GUI, I come up with banks. No file.
    ANy help or ideas?

    Cheers
    Rajiv

    ReplyDelete
    Replies
    1. Rajiv,
      Please mail me your code, I'll review.
      Regards,
      Sal

      Delete
    2. Rajiv,

      I'd check the total item count in the library or log in as the farm admin and check the library again. I had this problem before and it was because the documents needed to be checked in.

      Hope it helps!

      Delete
  4. This script worked great!!

    Thanks much!!!

    ReplyDelete
  5. The code works perfectly fine and pretty fast. Just one question though was, I was trying to associate multiple values with multi-value choice column as

    $Metadata = @{"Country" = "United States; India";}

    but this is not working. Any help would be appreciated.

    ReplyDelete
    Replies
    1. Ok. I found a way. We just need to use

      $Metadata = @{"Country" = ";#United States;#India;#";}

      Delete
  6. Dear Salaudeen Rajack,

    Many thanks for sharing such a wonderful blog.

    I am new to Power Shell. When I type it in sharepoint console. I am getting the following details. Can you help me in this regard.

    PS C:\Users\Administrator> Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAc
    tion SilentlyContinue
    PS C:\Users\Administrator> [System.Reflection.Assembly]::LoadWithPartialName("Mi
    crosoft.SharePoint")>$null
    PS C:\Users\Administrator> function UploadAllFilesFromDir($WebURL, $DocLibName,
    $FolderPath)
    >> {
    >> $site = New-Object Microsoft.SharePoint.SPSite($WebURL)
    >> $web=$site.OpenWeb()
    >> $List=$web.GetFolder($DocLibName)
    >> $Files = Get-ChildItem $FolderPath
    >> foreach ($File in $Files)
    >> {
    >> $stream = (Get-Item $file.FullName).OpenRead()
    >> $uploaded = $List.Files.Add($File.Name,$stream, $TRUE)
    >> $stream.Dispose()
    >> }
    >> $site.Dispose()
    >> UploadAllFilesFromDir "http://sp2010/sites/training" "Shared Documents" C:\Te
    st"
    >>

    Many thanks

    Warm Regards,
    Sathya

    ReplyDelete
    Replies
    1. Sathya, Just save the script in a file with .ps1 extension. Now you can run the script either by right clicking and choosing "Run with PowerShell" or from PowerShell Prompt!

      Delete
  7. Hi Salaudeen,
    Am looking for a similar script the only difference is that am trying to Importing/uploading files from file share to SP 2010 Doc Lib using a CSV which has all the metadata. Please advice. Thanks in advance.

    ReplyDelete
  8. Hi Salaudeen,
    I am trying to write a similar script. In my case I have an XLS file with respective metadata for all the files. For comparison, the name of file is same as the 1st column in XLS file.
    I am trying to find an efficient way to compare the values and store the related metadata somewhere. And use it for tagging the item uploaded.
    Thanks for any suggestions.

    ReplyDelete
    Replies
    1. hey Nayan, if u have found solution for it, plz share. I have the same need.

      Delete
  9. Hi Salaudeen,
    I am trying to write a similar script. In my case I have an XLS file with respective metadata for all the files. For comparison, the name of file is same as the 1st column in XLS file.
    I am trying to find an efficient way to compare the values and store the related metadata somewhere. And use it for tagging the item uploaded.
    Thanks for any suggestions.

    ReplyDelete
  10. Hi @salaudeen,

    i have 1000+ documents with distinct meta data, ur powersehell will update same metadata with all the documents. I have a excel carrying columns like
    FileName ... metadata1 ...metadata2 ... metadata3.

    Can we map the file name in directory and excel to update metadata. ?

    plz Help !!!

    ReplyDelete
  11. Hi Salaudeen,
    we have thousands of documents to upload with individual Metadata. As Nayan and Ankit already asked, it would be great to have a script reading those filenames and the subsequent metadata from an CSV or similar file.

    Your great experience is highly appreciated.

    Regards Wolfgang

    ReplyDelete
    Replies
    1. Hi There,

      Added the Script to get Files and Metadata from CSV file and do bulk upload!

      Delete
  12. Hi Salaudeen,

    I've been attempting to use your script for uploading metadata and files to sharepoint but I get a 'Parse errors detected' on the line where I'm trying to configure $SiteUrl.

    I think it's due to the number of quotes, I've checked it against your own script but haven't had any luck. Also I can't comment the line of code since it includes html and this comment box doesn't like that.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Hi,

    Thanks for the script. Does it delete the files and CVS file from the source folder once upload to Sharepoint is confirmed?
    Thanks.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...