Forum Discussion
Trouble with
I'm having a bear of a time getting this script to work. It's supposed to follow a specific SharePoint site for a specific user. As you can see from the code sample, I've made a lot of changes, REM'd out things already to get this thing to work. Currently, I'm stuck with this error:
"The term 'New-MgUserFollowedSite' is not recognized as a name of a cmdlet, function, script file, or executable program."
I've seen this error many other times, and it usually means the module isn't installed or outdated. In this case of uninstalled and reinstalled all of Microsoft.Graph, Microsoft.Graph.Sites, and Microsoft.Graph.Users. I've even tried updating them and importing them. Nothing works.
We are running modern SharePoint in the cloud.
Here's the code:
#Requires -Modules @{ModuleName='Microsoft.Graph.Users';ModuleVersion='2.6.0'}
# Install module if not already present: Install-Module Microsoft.Graph.Users -Scope CurrentUser
# Install-Module Microsoft.Graph.Users -Scope CurrentUser
# Install-Module Microsoft.Graph.Sites -Scope CurrentUser
# Configuration
$SiteURL = "https://XXXXXXXXX.sharepoint.com/sites/XXXXXXXX" # Replace with the actual site URL
$UserEmail = "email address removed for privacy reasons" # Replace with the user's email address
# Function to follow the site for a user
function Follow-SPOSite {
param(
[string]$SiteURL,
[string]$UserEmail
)
# Get the site ID
# $site = Get-MgSite -Filter "webUrl eq '$SiteURL'"
$siteId = "XXXXXXXXXXXXXXXXXXXXXX" # Replace with the actual site ID
# Get the user ID
$user = Get-MgUser -Filter "mail eq '$UserEmail'"
$userId = $user.Id
# Follow the site for the user
try {
New-MgUserFollowedSite -UserId $userId -OdataId "https://graph.microsoft.com/v1.0/sites/$siteId"
Write-Host "Successfully followed site '$SiteURL' for user '$UserEmail'." -ForegroundColor Green
} catch {
Write-Host "Error following site '$SiteURL' for user '$UserEmail': $($_.Exception.Message)" -ForegroundColor Red
}
}
# Connect to Microsoft Graph
try {
Connect-MgGraph -Scopes "User.Read.All", "Sites.ReadWrite.All"
} catch {
Write-Host "Error connecting to Microsoft Graph: $($_.Exception.Message)" -ForegroundColor Red
exit
}
# Follow the SharePoint site
Follow-SPOSite -SiteURL $SiteURL -UserEmail $UserEmail
# Disconnect from Microsoft Graph
Disconnect-MgGraph
You may ask why not just show the user how to follow sites. It's one of those political situations where someone high up is being intransigent about adopting SharePoint, and well I just have to find a way to follow sites for this one person.
Please disregard this post. I've made some progress with a new script and now have a different issue and posted about it here: PowerShell Script to Follow a SharePoint Site for a User | Microsoft Community Hub
1 Reply
- kcelmerBrass Contributor
Please disregard this post. I've made some progress with a new script and now have a different issue and posted about it here: PowerShell Script to Follow a SharePoint Site for a User | Microsoft Community Hub