3
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) { Add-PSSnapin Microsoft.SharePoint.PowerShell } $spWeb=Get-SPWeb -identity "http://sharepoint.contoso.com/Corporate/Sales/" $spFolder =$spWeb.GetFolder("arizona") $spFileCollection =$spFolder.Files Get-ChildItem "\Daily Reports\arizona\" -filter "*.xlsx" | ForEach { $spFileCollection.Add("arizona/$($_.Name)",$_.OpenRead(),$true) } $web.Dispose() 

My problem is arizona is not the top level folder but is under shared documents which is the top level document library.

How do I go about adding documents to my arizona folder from my network directory?

3
  • how to add the part to parse the title of the received documents ? Commented Aug 3, 2018 at 20:36
  • i need a powershell script to move/copy files from network path to SharePoint library . i dont have any folders inside. where to change the above script ? suppose if i want to move to this location only "Shared Documents" Please share ASAP Commented Aug 3, 2018 at 21:15
  • Can we do opposite? SharePoint Document Library to File Share? Commented Aug 23, 2018 at 16:07

2 Answers 2

5

use this powershell to upload to specific folder.

 if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null) { Add-PSSnapin Microsoft.SharePoint.PowerShell } #Script settings $webUrl = "http://sharepoint.contoso.com/Corporate/Sales/" $docLibraryName = "Shared Documents" $docLibraryUrlName = "Shared Documents\arizona" # specify your subfolder url here $localFolderPath = "C:\Test" #Open web and library $web = Get-SPWeb $webUrl write-host $webUrl $docLibrary = $web.Lists[$docLibraryName] write-host $docLibrary $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() write-host $files ForEach($file in $files) { if($file.Name.Contains(".pdf")) { write-host $file #Open file try { $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead() #Add file $folder = $web.getfolder($docLibraryUrlName) write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..." $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name,[System.IO.Stream]$fileStream, $true) write-host "Success" #Close file stream $fileStream.Close(); } catch { Write "Error: $file.name: $_" >>c:\logfile.txt continue; } } } #Dispose web $web.Dispose() 

this will add your files from local drive to Arizona folder from network drive c:/Test

3
  • You can also use Robocopy or a simple file copy with powershell and create the path of the document library. And then just copy from shared path to Sharepoint Shared Path. Looks like \\sharepoint-server@20502(portnumber of web app)\DavWWWRoot\sites\site-example\document-library and you can net use this path, and copy/paste. Don't forget to net use ... /DELETE. Commented Apr 24, 2014 at 6:58
  • I want to just upload certain type of files although there are different type of files in that folder . eg I want to upload all *.docx or *.doc , how do i do that Commented Apr 24, 2014 at 22:55
  • see my update answer. just replace .pdf with type of extension you want. also if you want more than one extension , than just use "-or" in the if loop Commented Apr 25, 2014 at 4:16
3

When adding files using PowerShell I prefer using Invoke-WebRequest with the PUT verb and then just specify the URL to where you want to store the file and its name.

1
  • @Ashish I want to just upload certain type of files although there are different type of files in that folder . eg I want to upload all *.docx or *.doc , how do i do that Commented Apr 24, 2014 at 16:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.