PnP PowerShell Update SharePoint Site Logo

Overview

This article will give you an idea about how to update the SharePoint Online site logo with the use of PnP (Patterns & Practices) PowerShell.

Pre-Requisite

  • Windows PowerShell
  • PnP SharePoint Online Management Module
    • Include module using following PowerShell command,
      Install-Module -Name SharePointPnPPowerShellOnline

Code

#Declare variables for poweshell script

#Specify site collection URL
$currentsite = "https://[tenantname].sharepoint.com/sites/[sitecollection]"; 

#Specify local folder path where image is available
$imgPath =  "[localfolderimagepath]";

#Keep it empty
$pathImg = ""

#Specify Design Library title to create on Online Sitecollection
$librarytitle = "Design Package";

#Specify Design Library URL name on Online Sitecollection
$libraryurl = "Designpackage"

#Connect SharePoint Online site
Connect-PnPOnline -Url $currentsite -UseWebLogin

#Create library to maintain all design packages
New-PnPList -Title $librarytitle -Template DocumentLibrary  -Url $libraryurl -ErrorAction SilentlyContinue

#Create folder inside library to maintain all images
Add-PnPFolder -Name images -Folder $libraryurl

#Upload logo file under folder
Add-PnPFile -Path $imgPath -Folder $libraryurl/images -ErrorAction Stop
Write-Host "Uploaded Logo file to : "$libraryurl

#Split file name from path
$imgName = $imgPath | Split-Path -Leaf

#get uploaded file path
$pathImg = (Get-PnPListItem -List $libraryurl -Fields FileRef).FieldValues | Where-Object {$_.FileRef -match $imgName} 

#Update site collection logo
Set-PnPWeb -SiteLogoUrl $pathImg.FileRef 
 
Write-Host "Updated Logo of: "$currentsite

#Get all subistes under site collection and update logo of all subsites
$subsites = Get-PnPSubWebs -Recurse
foreach ($site in $subsites){
   Connect-PnPOnline -Url $site.url -UseWebLogin
   Set-PnPWeb -SiteLogoUrl $pathImg.FileRef
   Write-Host "Updated Logo of: "$site.url
}
Disconnect-PnPOnline

Final Code 

#variables
$currentsite = "https://[tenantname].sharepoint.com/sites/[sitecollection]";
$imgPath =  "[localfolderimagepath]";
$pathImg = ""
$librarytitle = "Design Package";
$libraryurl = "Designpackage"

#Connect SharePoint Online site
Connect-PnPOnline -Url $currentsite -UseWebLogin

#Create library to maintain all design packages
New-PnPList -Title $librarytitle -Template DocumentLibrary  -Url $libraryurl -ErrorAction SilentlyContinue

#Create folder inside library to maintain all images
Add-PnPFolder -Name images -Folder $libraryurl

#Upload logo file under folder
Add-PnPFile -Path $imgPath -Folder $libraryurl/images -ErrorAction Stop
Write-Host "Uploaded Logo file to : "$libraryurl

#Split file name from path
$imgName = $imgPath | Split-Path -Leaf

#get uploaded file path
$pathImg = (Get-PnPListItem -List $libraryurl -Fields FileRef).FieldValues | Where-Object {$_.FileRef -match $imgName} 

#Update site collection logo
Set-PnPWeb -SiteLogoUrl $pathImg.FileRef 
 
Write-Host "Updated Logo of: "$currentsite

#Get all subistes under site collection and update logo of all subsites
$subsites = Get-PnPSubWebs -Recurse
foreach ($site in $subsites){
   Connect-PnPOnline -Url $site.url -UseWebLogin
   Set-PnPWeb -SiteLogoUrl $pathImg.FileRef
   Write-Host "Updated Logo of: "$site.url
}
Disconnect-PnPOnline