Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

Configuring Scopes when Connecting to the Microsoft Graph PowerShell API

July 25, 2024
in A, Blog, Microsoft Graph, Powershell
0
ADVERTISEMENT

Table of Contents

For many admins, the use of Scopes is a new addition to Office 365 PowerShell. The Scopes parameter was introduced with the Microsoft Graph PowerShell API, but the concept of Scopes is actually part of the OAuth2 specification.

The use of Scopes limits the user permissions available to an application or session.

By configuring Scopes when connecting to the Microsoft Graph PowerShell API, you effectively limit what permissions are available for the execution of commands. We’ll see this in a live example further down. But first, let’s consider the following:

Why Use Scopes?

For example, you want to run a script that exports some user information from the Microsoft 365 Graph. Since you’ll only be reading user information, it makes good sense to limit your permissions to read-only (principle of least privilege).

Connect-MgGraph -Scopes "User.Read.All"

This will ensure that you don’t accidentally make updates to user accounts. Also, it protects you from a rogue application trying to make changes when it should only be reading information.

Connecting to MS Graph With Scopes

To establish a connection with the Microsoft Graph PowerShell API to read user information, you can use the following command:

Connect-MgGraph -Scopes "User.Read.All"

This will cause OAuth2 authentication to kick in (unless you have already consented to the permissions requested in the Scopes parameter):

TmNGpxTXVaO2tDeGtbtlRwf9mxaOyRsSxcOVaRFsdLeL2HZ0oYubDtPKzJUb

Notice that you can clearly see what permissions you’re allowing the Microsoft Graph PowerShell application to use. In this case, the app only has read permissions to all accounts.

Additionally, you can consent on behalf of your organization. Granting consent will spare your coworkers from seeing the above permission request and allow Microsoft Graph PowerShell to immediately run commands on behalf of your coworkers.

Note Keep in mind: Consent does not grant any new permissions to users in your organization. Consent only allows the application (in this case, Microsoft Graph PowerShell) to use permissions already assigned to users. Read more about consent here.

After you accept the permissions request, the Microsoft Graph PowerShell application is configured with the new consent. Go to Enterprise Applications > Microsoft Graph Command Line Tools > Permissions > User consent to see it:

AU5tRy3N3R57Vv020xRuhsPynU0kdqZjeqEmC7Fkl4teHMK6GlW6B1VIyq1G

Testing Scope Settings on MS Graph PowerShell

When you’ve connected to Microsoft Graph, you can check the current permission is granted for the current session by using the Get-MgContext cmdlet:

PS C:\> (Get-MgContext).scopes
openid
profile
User.Read.All
email

To illustrate the protection received by using scopes configuration, let’s try to update an Entra ID user account by setting the UsageLocation attribute. We connect with Scopes set to User.Read.All and as expected, this fails:

PS C:\> Get-MgUser -UserId [email protected] -Property DisplayName,UsageLocation | ft DisplayName,UsageLocation

DisplayName UsageLocation
----------- -------------
Alex Wilber US

PS C:\> Update-MgUser -UserId [email protected] -UsageLocation "UK"

Update-MgUser : Insufficient privileges to complete the operation.
Status: 403 (Forbidden)
ErrorCode: Authorization_RequestDenied
Date: 2023-08-21T00:26:06
Headers:
Transfer-Encoding             : chunked
...

To fix this we have to reconnect using a more extensive scope. With the new scope, you can now the write permission on all user accounts on your tenant.

Connect-MgGraph -Scopes "User.ReadWrite.All"

Assuming we haven’t previously consented to this, we receive a new OAuth2 authentication dialog stating the new level of consent. This time, the app requires the read and write permission.

7sqQuUGLjqUxCtdxtmoIk9ZeqVf73j5wwkJ3YUPtxJYFwdaHnKmrRzrWAV6y

Once connected, when you check the MgContext, the new permission should be added.

PS C:\> (Get-MgContext).Scopes
openid
profile
User.Read.All
email
User.ReadWrite.All

After accepting this, the User consent of the Microsoft Graph PowerShell Enterprise Application is updated with the new consent which we can verify in the Azure Portal:

gAyhdDolalHjKS0qCJNWlYRqHLogLDmDpV1GWjiNnaCg1BjxN3BZrQ0QfisK

With the new scope in place, we are now able to write new information to user objects in Entra ID:

PS C:\> Update-MgUser -UserId [email protected] -UsageLocation "VN"

PS C:\> Get-MgUser -UserId [email protected] -Property DisplayName,UsageLocation | ft DisplayName,UsageLocation

DisplayName UsageLocation
----------- -------------
Alex Wilber VN

Finding Microsoft Graph Scopes

Finding the right scope can be a bit challenging at the beginning. But there are two good sources that you can use to determine which scopes you will need to specify:

  • Microsoft Graph Explorer: https://developer.microsoft.com/en-us/graph/graph-explorer
  • Microsoft Graph Rest API Reference: https://docs.microsoft.com/en-us/graph/api/overview

Microsoft Graph Explorer

The Microsoft Graph Explorer is a great tool to test out API calls to Microsoft Graph. It comes with a lot of examples calls to help you get started. But it will also list the required permission for the call.

Open the Graph Explorer | Select an Sample Query on the left side | Click Modify Permissions tab.

Bg1266

Microsoft Graph Rest API Reference

The other option is to use the Rest API Reference. You can select in the left menu one of the entities that you want to work with and then view the required permissions. You don’t need to add all scope, they are listed from least to most privileged.

An example, we find the scope to get OneDrive for Business for users.

Bg1267

Find-MgGraphCommand

Our favorite way to find the right scopes to connect to Microsoft Graph PowerShell is using Find-MgGraphCommand cmdlet.

For example, we found the right permission to update users information with Update-MgUser cmdlet.

(Find-MgGraphCommand -Command 'Update-MgUser').Permissions
Name                                         IsAdmin Description
----                                         ------- -----------
DeviceManagementApps.ReadWrite.All              True Read and write Microsoft Intune apps
DeviceManagementConfiguration.ReadWrite.All     True Read and write Microsoft Intune Device
DeviceManagementManagedDevices.ReadWrite.All    True Read and write Microsoft Intune devices
DeviceManagementServiceConfig.ReadWrite.All     True Read and write Microsoft Intune configuration
Directory.ReadWrite.All                         True Read and write directory data
User.EnableDisableAccount.All                   True Enable and disable user accounts
User.ManageIdentities.All                       True Manage  user identities
User.ReadWrite                                 False Read and update your profile
User.ReadWrite.All                             False Read and write all users' full profiles
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Connect to Microsoft Graph PowerShell API

Next Post

How to Fix the Term Select-MgProfile is not Recognized Error

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory