I have a script that generated a user report to a csv file. Which work properly. I have added a section to upload all the files from the local computer to the SharePoint library. Which also works fine. However, I just want to upload only new file. This is what i have so far. I changed "$FileCreationInfo.Overwrite = $false" from $true to $false. I guess I am missing a if exist statement. If someone can help me with the syntax to look at the folder and if the same file name already exist, do not upload.

Upload file

Foreach ($File in (dir $Folder)) { $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open) $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $false $FileCreationInfo.ContentStream = $FileStream $FileCreationInfo.URL = $File $Upload = $List.RootFolder.Files.Add($FileCreationInfo) $Context.Load($Upload) $Context.ExecuteQuery() }

this is the error I am getting.

Exception calling "ExecuteQuery" with "0" argument(s): "A file with the name Shared Documents/test.csv already exists. It was last modified by i:0#.f|membership|test on 19 Sep 2018 16:11:46 -0700." At C:\Scripts\test\test.ps1:68 char:1 + $Context.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServerException


You can use below script to check file

function IsFileExists()
try {
$File = $context.web.GetFileByServerRelativeUrl("/LibraryName/file.txt")
return $True
catch {
return $False

#File chech logic
$FileExists = IsFileExists
if($FileExists) {
#Dont upload file
else {
# upload the file
  • Nope that didn't work. Nothing was uploaded even the new file was created. – mgmjtech Sep 21 '18 at 15:34
  • can you your code after implementing the script – Sunil Sahu Sep 24 '18 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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