l pratham

l pratham

  • 1.2k
  • 509
  • 22.5k

how to upload multiple files in SP library powershell ?

Oct 9 2018 8:42 AM

trying to upload multiple files using powershell script in sharepoint library. using the below code im facing error.

error details 
 

powershell code
 

add

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)

{

Add-PSSnapin Microsoft.SharePoint.PowerShell

}

#Script settings

$webUrl = "site url"

$docLibraryName = "BSC"

$docLibraryUrlName = "BSC"

$localFolderPath = "local path"

#Open web and library

$web = Get-SPWeb $webUrl

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()

ForEach($file in $files)

{

#Open file

$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

#Add file

$folder = $web.getfolder($docLibraryUrlName)

$Metadata = @{"Document_Status" = "Ready For Review"; "Title" = "$file"}

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();

}

#Dispose web

$web.Dispose()

 
 
Thanks,
pratham. 

Answers (1)