I’ve tried the following code in PowerShell PnP to transfer a file to a SharePoint site. However, I don’t receive any errors, but my file does not get transferred.
I’ve tried every possible way of structuring the folder within $siteUrl and $libraryName, but nothing has worked.
I am authenticated to the server, but the file still does not get transferred.
$siteUrl = "https://mysp.sharepoint.com/teams/folder1/Org-folders"
$libraryName ="My team/PnP-Test"
$localFilePath = "C:TestFiletest.txt"
$checklocal = Test-Path -Path $localFilePath
Write-Host "Is the local file here" = $checklocal
#Define credentials
$username = "[email protected]"
$password = "MySecret"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
#Create credenntial object
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $securePassword
#Connect to PnP Online
Connect-PnPOnline -Url $siteUrl -Credentials $credentials
Write-Host "SharePoint Details below:"
Get-PnPSite
#powershell pnp to upload file to sharepoint online
$uploadedFile = Add-PnPFile -Path $localFilePath -Folder $libraryName -ErrorAction Stop
$checkremote = Test-Path -Path $uploadedFile
if ($checkremote){
Write-Host "File uploaded successfully."
#Get the path
$uploadedFilePath = $uploadedFile.ServerRelativeUrl
Write-Host "File uploaded to: $siteUrl-----$uploadedFilePath"}
Else{
$checkremote = Test-Path -Path $uploadedFile
Write-Host "Did the file get uploaded" = $checkremote}
Disconnect-PnPOnline
Output:
Is the local file here = True
SharePoint Details below:
[32;1mUrl [0m[32;1m CompatibilityLevel[0m
[32;1m--- [0m [32;1m------------------[0m
https://mysp.sharepoint.com/teams/folder1 15
Did the file get uploaded = False