2

I have following code

which allows me to transfer automatically one file to sharepoint. Is it possible to modify it the way it will upload every file from Temp to DL ? Thank you:

   # Set the variables
$WebURL = “http://contoso.com/spaces/”
$DocLibName = “Doc_DL”
$FilePath = “C:\Temp\PSW”

# Get a variable that points to the folder
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
$Files = $List.Files

# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

# Load the file into a variable
$File= Get-ChildItem $FilePath

# Upload it to SharePoint
$Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
$web.Dispose()

3 Answers 3

1

Untested, but it would be something like this:

# Set the variables
$WebURL = "http://contoso.com/spaces/"
$DocLibName = "Doc_DL"
$FilePath = "C:\Temp\PSW"

# Get a variable that points to the folder
$Web = Get-SPWeb $WebURL
$List = $Web.GetFolder($DocLibName)
$ListFiles = $List.Files

# Get just the name of the file from the whole path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1)

# Load the file into a variable
$Files = Get-ChildItem $FilePath

# Upload it to SharePoint
foreach($File in $Files){
  $ListFiles.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
}
$web.Dispose()
2
  • Unfortunitly notI'v git error: a file with the name Doc_DL already exist Feb 17, 2017 at 14:36
  • again, it was untested, but that should be the structure, you might have to change the $false parameter to $true Feb 17, 2017 at 14:42
0

I have found two intresting codes that resolve my issue:

Copy Files from folder

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

#Script settings
$webUrl = "http://vs-server12"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared%20Documents"
$localFolderPath = "C:\temp\Docs4SharePoint"

#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)

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

And this one copy folders, subfolders and files.

https://gallery.technet.microsoft.com/office/Upload-local-folder-its-7c6f6a06

0

I recommend to use PnP PowerShell, you can handle multiple auth scenarios over there including MFA (multi factor authentication)

This link might help: https://thenikhilk.com/2020/08/30/upload-to-sharepoint/

It has two versions of PowerShell scripts which even takes care of MFA (2FA)

It only takes a few parameters, you can even use it in your pipelines or automation and it supports all SharePoint versions

  • Online
  • 2019
  • 2016
  • 2013

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.