Upload file to SharePoint Online with PnP.PowerShell

Hi All,

A few weeks ago i had to upload a File to a SharePoint Site with a PowerShell Script.
In this Blog Article i explain how i did it.

All you need is

Here is the File i want to upload and replace with my PowerShell Script (Documents/Project/Script/AADUsers.csv)


Azure AD Application

You need to create an Azure AD Application. Copy the Application ID, you will need that later for the PowerShell Script


The Application need to have a ClientSecret. Copy the ClientSecret, you will need that later for the PowerShell Script.
Sadly you can't use Certificates with PnPPowerShell for Authentication.


You don't need any Permissions. These will be set in the Sharepoint Site.



SharePoint Permission

Go to the Sharepoint Site you want to Upload open the "/_layouts/appinv.aspx" and enter the App ID and klick on lookup.
https://[tenant].sharepoint.com/sites/[siteName]/_layouts/appinv.aspx


Now we add the Permission. It has to be done with an XML File

Add-in permissions in SharePoint
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/add-in-permissions-in-sharepoint

http://sharepoint/content/tenant                     #Tenancy
http://sharepoint/content/sitecollection             #Site Collection   
http://sharepoint/content/sitecollection/web        #Website
http://sharepoint/content/sitecollection/web/list    #List

In this Example the Following XML is Sufficient

<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web/list" Right="FullControl"/>
</AppPermissionRequests>


On the next Page we select "Documents" and hit "Trust it"


You can't change the Settings. But under the Site Settings > Site collection App permissions you can view the Applications


As mentioned, you can't edit. Simply delete the App.


PowerShell Script

And here is the PowerShell Script to Upload a File with PnPPowerShell

###############################################################################
# Upload file to SharePoint with PnP.PowerShell
# 23.01.2022 - Andres Bohren
###############################################################################
#Variables
$AppID = "0d1c73de-c74d-4b06-8a35-e53c8e190258"
$ClientSecret = "YourClientSecret"
$SiteURL = "https://icewolfch.sharepoint.com/sites/DemoTemplate/"
$FileURL = "Freigegebene Dokumente/Project/Script/AADUsers.csv"

#Connect-PnPOnline
Write-Output "Connect-PnPOnline"
Connect-PnPOnline -Url $SiteURL -ClientId $AppID -ClientSecret $ClientSecret -WarningAction Ignore
Get-PnPContext

#Items in Folder
$RelativeURL = "Freigegebene Dokumente/Project/Script"
$Items = Get-PnPFolderItem -FolderSiteRelativeUrl $RelativeURL
$Items

#Upload File
$CSVFile = "C:\GIT_WorkingDir\PowerShellScripts\SharePoint\AADUsers.csv"
Write-Output "Uploading CSV to Sharepoint"
$FolderObject = Get-PnPFolder -Url $RelativeURL
$Upload = Add-PnPFile -Path $CSVFile -Folder $FolderObject
If ($Upload -ne $null)
{
    Write-Output "File sucessfully uploaded"
}






Regards
Andres Bohren