Hello Community,
I am working in SP 2016 with a PowerShell script that is designed to upload documents from a local folder to a SP List. I got the code from this site (the full code is below):
https://camerondwyer.com/2013/05/27/how-to-import-upload-an-entire-folder-of-files-to-sharepoint-using-powershell/
Now the code works just fine except for one thing - this line - "$web = Get-SPWeb $webUrl" is always null, that is the object is empty, and because of that, no destination location is set and no documents are uploaded. Has anyone else seen this problem? If so please offer guidance and code examples that will help me fix the problem.
if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{ Add-PSSnapin Microsoft.SharePoint.PowerShell
Write-Host "SP Add-in Activated"
}
#Script settings
$webUrl = "https://myspsite/sites/CMTestUpload"
$docLibraryName = "SSOs_Test"
$docLibraryUrlName = "SSOs_Test"
$localFolderPath = "D:\UploadFiles\SSOs"
#Open web and library
$web = Get-SPWeb $webUrl
Write-Host "webSite = " + $webSite
$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()Thanks!
tOM
Tom Molskow - Senior SharePoint Architect - Microsoft Community Contributor 2011 and 2012 Award - Linked-In - SharePoint Gypsy