Automating with PowerShell: Automatically following all Sharepoint Sites or Teams for all users

So a while back we had a client that uses a lot of sharepoint sites. The client only used Sharepoint online, and found it hard to find all the sites in one place. We pointed them to which gives a nice overview of sites and teams.

They came back to us saying it was a little bit of a hassle to use the overview as it only shared recently used sites, or sites that have been followed manually. Of course we wanted to help them get over this hassle so we scripted this; The following script allows you to grab all sites the user is a member of. It then adds the site to the favorites for that user. You can schedule this so each new user automatically gets added.

The Script

The script uses the Secure Application Model or just a generic Azure Application with permissions to read all sites for your tenants, and all your CSP tenants. The script finds each Team the user has joined and adds them to the favorites for that user.

$ApplicationId = "YOURPPLICTIONID"
$body = @{
    'resource'      = ''
    'client_id'     = $ApplicationId
    'client_secret' = $ApplicationSecret
    'grant_type'    = "client_credentials"
    'scope'         = "openid"

$ClientToken = Invoke-RestMethod -Method post -Uri "$($tenantid)/oauth2/token" -Body $body -ErrorAction Stop
$headers = @{ "Authorization" = "Bearer $($ClientToken.access_token)" }
$Users = (Invoke-RestMethod -Uri "" -Headers $Headers -Method Get -ContentType "application/json")
foreach ($userid in $users) {
    $AllTeamsURI = "$($UserID)/JoinedTeams"
    $Teams = (Invoke-RestMethod -Uri $AllTeamsURI -Headers $Headers -Method Get -ContentType "application/json").value
    foreach ($Team in $teams) {
        $SiteRootReq = Invoke-RestMethod -Uri "$($" -Headers $Headers -Method Get -ContentType "application/json"
        $AddSitesbody = [PSCustomObject]@{
            value = [array]@{
                "id" = $
        } | convertto-json
        $FavoritedSites = Invoke-RestMethod -Uri "$($userid)/followedSites/add" -Headers $Headers -Method POST -body $AddSitesBody -ContentType "application/json"
        write-host "Added $($SiteRootReq.webURL) to favorites for $userid"


And that’s it! As always, Happy PowerShelling!


  1. Casey April 29, 2021 at 5:18 pm

    Getting this error: Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
    At line:15 char:11
    + $Users = (Invoke-RestMethod -Uri “ …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

    What should I Do?

  2. Alex June 3, 2021 at 10:04 pm

    api permissions

    Add permission – ‘Microsoft graph – application permission

    – Sites.ReadWrite.All

    – User.Read.All

  3. Aleksi July 23, 2021 at 11:08 am

    Thanks, perfect! This is exactly what I was looking for.

  4. Jeroen November 3, 2021 at 3:53 pm


    It works perfect, but how to fetch more than 100 users?

  5. JC November 21, 2021 at 7:00 pm

    im sorry, im not an expert on powershell, where do i get the applicationID and the application secret? i have a bunch of teams and multiple mobile users who dont use any desktop applications and they need to access their libraries on the onedrive app.


Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.