Forum Discussion
PowerShell Script to Follow a SharePoint Site for a User
Hi kcelmer
I did play around a little bit with Interactive Permissions (like in your example).
I was not able to add or remove a Follower (other than my own user)
###############################################################################
# Connect with MgGraph Interactive
###############################################################################
Connect-MgGraph -Scopes "Sites.ReadWrite.All","User.Read.All" -NoWelcome
#Get User
$UPN = "email address removed for privacy reasons"
$User = Get-MgUser -UserId $UPN
Write-Host "UserID: $($user.id)" -ForegroundColor Cyan
#Details of SharePoint Site
$SiteURL = "https://icewolfch.sharepoint.com/sites/DemoPrivate"
$Domain = ([System.Uri]$SiteURL).Host
Write-Host "Domain: $Domain" -ForegroundColor Cyan
$AbsolutePath = ([System.Uri]$SiteURL).AbsolutePath.split("/")[2]
Write-Host "$AbsolutePath" -ForegroundColor Cyan
$uriSite = [string]::Format('https://graph.microsoft.com/v1.0/sites/{0}:{1}',$Domain,$AbsolutePath)
$Site = Invoke-MgGraphRequest -Method GET $uriSite
Write-Host "SiteID: $($site.id)" -ForegroundColor Cyan
#Create Body for Add/Remove
$params = @{
value = @(
@{
id = $Site.ID
}
)
}
#Create Body for Add/Remove
$params = @{
value = @(
@{
id = $Site.ID
}
)
}
#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
Tried with an Entra App and Certificate for Authentication.
Be aware that List followed sites is not Supported with Application Permissions
https://learn.microsoft.com/en-us/graph/api/sites-list-followed?view=graph-rest-1.0&tabs=http
###############################################################################
# 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
#Details of SharePoint Site
$SiteURL = "https://icewolfch.sharepoint.com/sites/DemoPrivate"
$Domain = ([System.Uri]$SiteURL).Host
Write-Host "Domain: $Domain" -ForegroundColor Cyan
$AbsolutePath = ([System.Uri]$SiteURL).AbsolutePath.split("/")[2]
Write-Host "$AbsolutePath" -ForegroundColor Cyan
$uriSite = [string]::Format('https://graph.microsoft.com/v1.0/sites/{0}:{1}',$Domain,$AbsolutePath)
$Site = Invoke-MgGraphRequest -Method GET $uriSite
Write-Host "SiteID: $($site.id)" -ForegroundColor Cyan
#Create Body for Add/Remove
$params = @{
value = @(
@{
id = $Site.ID
}
)
}
#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
Hope that helps.
Kind Regards
Andres
- kcelmerMay 09, 2025Brass Contributor
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?
- Andres-BohrenMay 13, 2025Iron Contributor
Hi kcelmer
I found an error in the codeThe 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>/followedSitesAnd 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.