Upload file to SharePoint Document library using powershell
Here is simple script in layman style which is tested and working fine to upload files from your drive to SharePoint document library
http://soreddymanjunath.blogspot.in/2014/07/add-file-to-document-library-using.html
clsasnp "*sh*"$url=Read-Host "Enter Site Url" $web=Get-SPWeb -Identity $urlif($web){try{$list = $web.Lists.TryGetList("Documents")$files = Get-ChildItem -Path "D:\Manju" -Force -Recurse foreach ($file in $files) { $stream = $file.OpenRead() $done= $list.RootFolder.Files.Add($file.Name, $stream, $true) Write-Host $done.Name "Uploaded into the Site" -BackgroundColor Green }}catch{$ErrorMessage = $_.Exception.MessageWrite-Host $ErrorMessage}}else{Write-Host "Site Doesn't exist"}$list.Update();
Thank you so much for the response, I tried to do the same way you suggested.
Add-PSSnapin Microsoft.SharePoint.PowerShell [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null$spWeb = Get-SPWeb -Identity "//abc/enterprise/en-sg/RenderingAssets/" $spFolder = $spWeb.GetFolder("oneMSCOM") $spFileCollection = $spFolder.Files Get-ChildItem "D:\test" -filter ?*.txt? | ForEach { $spFileCollection.Add("oneMSCOM/$($.Name)",$.OpenRead(),$true) }function global:Get-SPSite($url){ return new-Object Microsoft.SharePoint.SPSite($url) }$siteColletion = Get-SPSite("://abc/enterprise/en-sg/RenderingAssets/");$folder = $siteColletion.RootWeb.Folders["Upload Demo"];$collFiles = $folder.Files;for ($intIndex=0; $intIndex -ne $folder.Count; $intIndex++) { if ($folder[$intIndex].CheckedOutBy.LoginName -eq "FAREAST\v-kisriv") { $folder[$intIndex].CheckIn(""); }}$siteColletion.Dispose()
In this the complete URL where file should be upload is :
://abc/enterprise/en-sg/RenderingAssets/oneMSCOM/RenderingAssets : Document LibrayOneMSCOM : folder.
I ran this script successfully on my server. It uploads all documents from C:\Users\student\Documents\My Documents
with a docx
file extension to a document library called Upload Demo
. After upload it checks in all the documents in that library.
I used scripts from these two references:
- http://consultingblogs.emc.com/robertoortega/archive/2012/03/03/checking-in-all-files-in-a-document-library-from-powershell.aspx
- http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/23/use-powershell-cmdlets-to-manage-sharepoint-document-libraries.aspx
.
Add-PSSnapin Microsoft.SharePoint.PowerShell [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null$spWeb = Get-SPWeb -Identity "http://mia-sqlbi.adventureworks.msft/"$spFolder = $spWeb.GetFolder("Upload Demo")$spFileCollection = $spFolder.Files Get-ChildItem "C:\Users\student\Documents\My Documents" -filter ?*.docx? | ForEach { $spFileCollection.Add("Upload Demo/$($_.Name)",$_.OpenRead(),$true) } function global:Get-SPSite($url) { return new-Object Microsoft.SharePoint.SPSite($url)}$siteColletion = Get-SPSite("http://mia-sqlbi.adventureworks.msft/");$folder = $siteColletion.RootWeb.Folders["Upload Demo"];$collFiles = $folder.Files;for ($intIndex=0; $intIndex -ne $folder.Count; $intIndex++) { if ($folder[$intIndex].CheckedOutBy.LoginName -eq "adventureworks\student") { $folder[$intIndex].CheckIn(""); }}$siteColletion.Dispose()
Here's a screen shot of what it should look like: