Microsoft 365 – SharePoint Online – PnP PowerShell – To install app on given SharePoint online sites

Hi All,
Greetings for the day!!!
Today new PowerShell – Installing App using PnP PowerShell on SharePoint Online site
Use Case / Background
- In our tenant we have multiple modern communication site collections and subsites
- We need to add google analytics script on our sites
- So we have written App which installs scripts
- We have uploaded our App in tenant app catalog
- Now we need to install this App on the multiple site collections / subsites where we need it
Solution
- So here we thought to have PowerShell script
- Our script will iterate through CSV file having URLs of our SharePoint online sites on which we need to install the respective given APP
So we prepared the CSV file having column “URL” of sites where we need to install the APP as

PowerShell script
- Initialize the required variables as
- CSV file path
- Success log file path – In this file we are maintaining logs / URLs of sites on which app is installed successfully
- Error log file path – In this file we are maintaining logs / URLs of sites on which app is not installed successfully
- App name – which we need to install – here we are installing app – “Image Map”
- Credential file path – secrets.txt – we are reading credentials from file stored on local drive
#CSV file path
$csvFilePath = "C:\Prasham\Articles\PowerShell\sitecollections.csv"
#success log file path
$logFileName = "C:\Prasham\Articles\PowerShell\successlog.csv"
#error log file path
$errorlogFileName = "C:\Prasham\Articles\PowerShell\errorlog.csv"
$AppName = "Image Map"
#get the credentials - currently reading from text file - screts.txt
#we are using Get-Content PowerShell CMDLET to read file
#in credential file - secrets.txt - we are defining user name and password
$getCreds = Get-Content -Path "C:\Prasham\Articles\PowerShell\secrets.txt"
- Here we are using Get-Content PowerShell CMDLET to read our username / password file from local drive – secret.txt
- We have detailed article on how to read text file – Small Tips and Tricks – PowerShell – how to read content from text file – https://knowledge-junction.in/2023/02/10/small-tips-and-tricks-powershell-how-to-read-content-from-text-file/
- As we are reading credential file, we will have username and password as follows
$UserName = $getCreds[0]
$Password = $getCreds[1]
- Next step is to create credential object as
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword
- Next step is to read the CSV file having list of site collections for which we need to install the respective app as
- We will use Import-CSV PowerShell CMDLET to get the content of CSV file
- We have detailed article for reading CSV file – on Import-CSV – Read a CSV file with PowerShell using the Import-CSV function – https://knowledge-junction.in/2022/10/29/read-a-csv-file-with-powershell-using-the-import-csv-function/
#read the CSV file having URLs of all sites on which we need to hide the title
$sites = Import-CSV $csvFilePath
- We will iterate through each site
foreach($site in $sites) {
$url = $site
- Here we are using PnP CMDLETS
- We will connect to the site using – Connect-PnPOnline as
Connect-PnPOnline -Url $url.URL -Credentials $Cred
- As we connected to our tenant, we have CMDLET – to get the app as
#Get the App from App Catalog using PnP CMDLET - Get-PnPApp
$App = Get-PnPApp -Scope Tenant | Where {$_.Title -eq $AppName}
- As we have app, we will install the app as
#Install App to the Site using PnP CMDLET - Install-PnPApp
Install-PnPApp -Identity $App.Id
- Maintain the success logs as
#generate success log
Write-Host "app deployed for Site collection $url successfully :)"
Add-Content $logFileName -Value " app for Site collection $url.URL successfully :)"
- We will also maintain error logs as
catch{
#generate error log
Write-Host “app for Site collection $url failed :(” $($_.Exception.Message)
Add-Content $errorlogFileName -Value “app deployment for Site collection failed – $site $($_.Exception.Message)”
continue;
}
COMPLETE SCRIPT
<#
.SYNOPSIS
This script reads CSV file having column – URL
.DESCRIPTION
This script reads CSV file having column - URL - and install the given APP for the site collection / subsite with given URL
.NOTES
Author: Prasham Sabadra
Version 1.0 - initial release
#>
#intialise variables
#CSV file path
$csvFilePath = "C:\Prasham\Articles\PowerShell\sitecollections.csv"
#success log file path
$logFileName = "C:\Prasham\Articles\PowerShell\successlog.csv"
#error log file path
$errorlogFileName = "C:\Prasham\Articles\PowerShell\errorlog.csv"
$AppName = "Image Map"
#get the credentials - currently reading from text file
$getCreds = Get-Content -Path "C:\Prasham\Articles\PowerShell\secrets.txt"
#getting user name and password from file
$UserName = $getCreds[0]
$Password = $getCreds[1]
#creating credential object
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword
#read the CSV file having URLs of all sites on which we need to hide the title
$sites = Import-CSV $csvFilePath
#iterating to each site from CSV file
foreach($site in $sites) {
$url = $site
try{
#connecting to our site
Connect-PnPOnline -Url $url.URL -Credentials $Cred
#Get the App from App Catalog
$App = Get-PnPApp -Scope Tenant | Where {$_.Title -eq $AppName}
#Install App to the Site
Install-PnPApp -Identity $App.Id
#generate success log
Write-Host "app deployed for Site collection $url successfully :)"
Add-Content $logFileName -Value " app for Site collection $url.URL successfully :)"
}
catch{
#generate error log
Write-Host "app for Site collection $url failed :(" $($_.Exception.Message)
Add-Content $errorlogFileName -Value "app deployment for Site collection failed - $site $($_.Exception.Message)"
continue;
}
}#foreach($site in $sites)

Thanks for reading 🙂 HAVE a FANTASTIC LEARNING 🙂 LIFE IS BEAUTIFUL 🙂
You must be logged in to post a comment.