Forum Discussion
PowerShell Script to Follow a SharePoint Site for a User
Thank you Andres! It looks like you put considerable effort into this. Just to clarify, were you able to follow a site for a user with the Entra/certificate approach?
Hi kcelmer​
I found an error in the code
The Graph Endpoint sites only returns the "Team site (classic experience)"
https://graph.microsoft.com/v1.0/sites/<tenant>.sharepoint.com
If you use the following command you get the Websites with the ID's
Get-MgSite | where {$_.DisplayName -match "demo"}
Get-MgSite | where {$_.DisplayName -eq "IcewolfDemo"}
###############################################################################
# Connect with Entra Application
###############################################################################
# Application Permissions
# - Sites.ReadWrite.All
# - User.ReadBasic.All
###############################################################################
$AppID = "2f79c9c9-4024-4d46-a06f-67c1f2d92b02"
$TenantID = "icewolfch.onmicrosoft.com"
$CertThumbprint = "A3A07A3C2C109303CCCB011B10141A020C8AFDA3"
Connect-MgGraph -AppId $AppID -TenantId $TenantID -CertificateThumbprint $CertThumbprint -NoWelcome
#Get User
$UPN = "email address removed for privacy reasons"
$User = Get-MgUser -UserId $UPN
Write-Host "UserID: $($user.id)" -ForegroundColor Cyan
$SiteID = "icewolfch.sharepoint.com,e5167e43-7495-4611-b74c-bbf2ffd85ce5,0c772746-d2d9-4c13-8176-bd41df1b7a6e" #IcewolfDemo
#Create Body for Add/Remove
$params = @{
value = @(
@{
id = $SiteID
}
)
}
#Add Follower
Write-Host "Add Follower to Site: $($Site.Id)" -ForegroundColor Cyan
Add-MgUserFollowedSite -UserId $user.Id -BodyParameter $params
#Remove Follower
#Write-Host "Remove Follower to Site: $($Site.Id)" -ForegroundColor Cyan
#Remove-MgUserFollowedSite -UserId $user.Id -BodyParameter $params
The Graph Query on the User is updated
https://graph.microsoft.com/v1.0/users/<userprincipalname>/followedSites
And the Followed Sites in SharePoint is reflecting that (takes a few Minutes until that's visible here)
So yes, it works but you have to figure out the SiteID and use that as a Parameter.
Kind Regards
Andres
- kcelmerMay 13, 2025Brass Contributor
Thank you! I will give this a try.