Reputation: 61
Hello I am trying to upload a file to my my Sharepoint Site (root directory) via PowerShell. I can connect to the url but having issues with when trying to upload the file i have installed Install-Module SharePointPnPPowerShellOnline
when i run the command i get this error
Add-PnPFile : The remote server returned an error: (403) Forbidden. At line:1 char:1 + Add-PnPFile -Path D:\delme\test.xlsx -Folder / + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (:) [Add-PnPFile], WebException + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Files.AddFile
Add-PnPFile -Path D:\delme\test.xlsx -Folder /
I know I am close.
Upvotes: 0
Views: 2012
Reputation: 49
Hi You can use this code to upload files and check ether the file is successfully updated or not
Code -
Import-Module SharePointPnPPowerShellOnline
#Get Connection to the url via Connect-PnPOnline $URL
Connect-PnPOnline "Some SharePoint Url" -UseWebLogin
#store all the files in the folder(get files using get-ChildItem)
#for single file you can use something like : $file = LocalPath\fileName.txt
$Files = Get-ChildItem "Local Folder Path which contains the files"
#Looping through each item
foreach($File in $Files){
#storing the add-pnpFile in a variable so that i will be able to know the status, if my file have been successfully uploaded or not.
#"Shared Documents\If inside shared document you want to give some folder it will come here" else only "Shared Document"
$upload = Add-PnPFile -Folder "Shared Documents\FolderToUpload" -Path $File.FullName
#now checking, if file successfully uploaded or not and printing the required message. (You can use Write-Host $message if you want to print the message)
if($upload.UniqueId){
$message = "Successfully Uploaded"
}
else {
$message = "ERROR - Unable to Upload"
}
}
Upvotes: 0
Reputation: 4228
Use the PowerShell below.
$siteurl="https://ABC123.sharepoint.com/sites/XXX/teamsites/os"
Connect-PnPOnline -Url $siteurl
Add-PnPFile -Path D:\delme\test.xlsx -Folder "Directory and Operating Systems"
Upvotes: 0