I am to run following script on SharePoint Online Management Shell, to bulk upload documents with metadata, but getting error(below screen shots)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration Variables
$SiteURL="https://tenant.sharepoint.com/sites/CMS/"
$LibraryName="TestLibrary"
$FilesPath="C:\Users\userX\Desktop\Upload Test 4 sug\NaUpload"
$CSVFilePath="C:\Users\userX\Desktop\Upload Test 4 sug\Nature.csv"
#Get the Web
$spWeb = Get-SPWeb $SiteURL
#Get the Target Document Library to upload
$Library = $Web.GetFolder($LibraryName)
#Get CSV file contents
$CSVData = Import-CSV -path $CSVFilePath
#Iterate through each Row in the CSV
Foreach ($Row in $CSVData)
{
write-host "Uploading File:"$Row.FileName
#Get the File from local disk
$SourcePath= Join-Path $FilesPath -ChildPath $Row.FileName
$SourceFile = (Get-ChildItem $SourcePath).OpenRead()
#Set Metadata Hashtable For the file
$Metadata = @{"Document No" = $Row.Document_x0020_No; "Title" = $Row.Title}
#upload the file
$uploaded = $Library.Files.Add($Row.FileName, $SourceFile, $Metadata, $TRUE)
}
Error:
After, input from @M.Qassas, I made some changes in my script and now I am getting Null-Valued Expression Error
Add-PSSnapin Microsoft.Online.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration Variables
$SiteURL= "https://tenant.sharepoint.com/sites/CMS"
$LibraryName= "TestLibrary"
$FilesPath= "C:\Users\userX\Desktop\Upload Test 4 sug\NaUpload"
$CSVFilePath= "C:\Users\userX\Desktop\Upload Test 4 sug\Nature.csv"
#Get the SpoWeb
$Web = Get-SPOSite $SiteURL
#Get the Target Document Library to upload
$Library = $Web.web.list.GetByTitle($LibraryName)
#Get CSV file contents
$CSVData = Import-CSV -path $CSVFilePath
#Iterate through each Row in the CSV
Foreach ($Row in $CSVData)
{
write-host "Uploading File:"$Row.Name
#Get the File from local disk
$SourcePath= Join-Path $FilesPath -ChildPath $Row.Name
$SourceFile = (Get-ChildItem $SourcePath).OpenRead()
#Set Metadata Hashtable For the file
$Metadata = @{"Document No" = $Row.Document_x0020_No; "Title" = $Row.Title}
#upload the file
$uploaded = $Library.Files.Add($Row.Name, $SourceFile, $Metadata, $TRUE)
}
Error: