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
- Azure AD Application
- Sharepoint Permission
- PnPPowerShell https://www.powershellgallery.com/packages/PnP.PowerShell/
Here is the File i want to upload and replace with my PowerShell Script (Documents/Project/Script/AADUsers.csv)
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_01.jpg)
Azure AD Application
You need to create an Azure AD Application. Copy the Application ID, you will need that later for the PowerShell Script
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_02.jpg)
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.
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_03.jpg)
You don't need any Permissions. These will be set in the Sharepoint Site.
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_05.jpg)
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
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_06.jpg)
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
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>
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_07.jpg)
On the next Page we select "Documents" and hit "Trust it"
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_08.jpg)
You can't change the Settings. But under the Site Settings > Site collection App permissions you can view the Applications
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_09.jpg)
As mentioned, you can't edit. Simply delete the App.
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_10.jpg)
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"
}
# 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"
}
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_11.jpg)
![](https://icewolffile.blob.core.windows.net/$web/202201/SharepointApp_UploadFile_12.jpg)
Regards
Andres Bohren
![](https://icewolffile.blob.core.windows.net/$web/logos/O365_logo.png)