Kaikus
Kaikus

Reputation: 1095

Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 1-Using PnP.Powershell)

I'm trying to upload a file into a Sharepoint Online (M365) library subfolder, but it keeps giving me errors. I have tried many scripts. This post is about using PnP.Powershell (I have posted questions about other scripts hoping someone can help me with any of them)

This is the code:

$url="https://mydomain.sharepoint.com/sites/mysharepointsite/" 

$userID = "[email protected]"
$securePassword = ConvertTo-SecureString -String "myPaswword" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $userID, $securePassword

Install-Module -Name PnP.PowerShell
Connect-PnPOnline $url -Credentials $credentials

$files = Get-Item -Path "C:\myFolder\myFile.csv" -Force
foreach ($file in $files)
{
    Add-PnPFile -Folder "myLibraryname/subfolder"  -Path $file.FullName
    Write-Host $done.Name  "Uploaded into the Site" -BackgroundColor Green
}

It gives me this error

Connect-PnPOnline : AADSTS65001: The user or administrator has not consented to use the application with ID 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' named 'PnP Management Shell'. Send an interactive authorization request for this user and resource.

How do I grant this permission in M365? Is it safe to grant it?

Thanks

Upvotes: 1

Views: 1696

Answers (1)

Steve B
Steve B

Reputation: 37660

PnP PowerShell need to be granted some privileges on your tenant.

Maybe you can try

Register-PnPManagementShellAccess

Ref: Connecting with PnP PowerShell

I guess it's safe if you ensure you are on the official build and not a customized (hacked) one.

Basically, it will grant the module the right to perform some task using a delegated authorization. It won't have more privileges than your account.

Upvotes: 1

Related Questions