Hello Stephan Bren,
I understand you are trying to get the SharePoint webUrl
of a Microsoft 365 (Unified) group using Microsoft Graph PowerShell. The first two steps in your example are correct, but the third line fails due to how Get-MgGroupSite
behaves when no site ID is provided.
Initially, I too got same error when I ran your commands without passing site ID:
Connect-MgGraph -Scopes "Group.Read.All", "Sites.Read.All"
$group = Get-MgGroup -GroupId "xxxxxxxxxxxxxx"
$Site=Get-MgGroupSite -GroupId $group.Id
When this command is run without -SiteId
, it internally calls the getAllSites()
API. You can confirm this behavior by adding the -Debug
switch:
$Site=Get-MgGroupSite -GroupId $group.Id -Debug
This particular API requires application permissions and does not work with user-based (delegated) permissions. That’s why it returns a 403 Access Denied
. Check this MS Article.
To retrieve the group's primary SharePoint site using delegated permissions, you can call the /sites/root
endpoint directly like this:
Connect-MgGraph -Scopes "Group.Read.All", "Sites.Read.All"
$groupId = "<your-group-id>"
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/groups/$groupId/sites/root"
$site.webUrl
If you already know the site's name or relative path, another option is to use Get-MgSite
to retrieve the site ID and then pass it into Get-MgGroupSite
. This skips the getAllSites()
call and works correctly with delegated access:
$site = Get-MgSite -SiteId "yourtenant.sharepoint.com:/sites/yourgroupname:"
$groupSite = Get-MgGroupSite -GroupId $groupId -SiteId $site.Id
$groupSite.WebUrl
Let me know if you have further queries. Hope this helps!
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful, which may help members with similar questions.
If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.