I'm trying to use Microsoft Graph API in Powershell to pull a list of sharepoint sites a user is Owner of but I am getting a (403) Forbidden error

Joe Moorcroft 5 Reputation points
2025-02-13T13:23:20.0133333+00:00

I've created an App in Entra so I can use it to give me a list of sharepoint sites a certain user is owner of. I have used Copilot to help me write the powershell script and I have all the needed permissions on my account to do what I need.

Soon as I run the script, I get the following error:

Invoke-RestMethod : The remote server returned an error: (403) Forbidden.

At line:26 char:20

  • ... teDetails = Invoke-RestMethod -Method Get -Uri "https://graph.microso ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Now I'm new to this stuff so I don't quite get it entirely so I've come here to see if someone could help me figure this out. If I need to supply more info then let me know.

Thank you

Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vasil Michev 119.5K Reputation points MVP Volunteer Moderator
    2025-02-13T17:10:31.2+00:00

    Share the script please. When it comes to the Graph SDK, the most important thing is permissions. You need to specify which permissions are needed, and provide consent for them as part of the connectivity process. Something like this:

    Connect-MgGraph -Scopes Users,Read,All,Sites.Read.All
    

    The exact permissions will depend on the cmdlets leveraged in the script.

    Another thing to note is that it comes to enumerating SharePoint Online sites, currently only application permissions are supported. You cannot connect as a user that is, you need to run the cmdlet via service principal. See this article for details: https://learn.microsoft.com/en-us/powershell/microsoftgraph/app-only?view=graph-powershell-1.0


  2. CarlZhao-MSFT 46,366 Reputation points
    2025-02-14T02:54:05.27+00:00

    Hi @Joe Moorcroft

    Are there any other API calls in the current context? Try adding the Directory.ReadWrite.All application permission and retry.

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.