10 Great Tips for Admins Working With Microsoft Graph PowerShell

  • Post author:
  • Post category:Microsoft Graph
  • Post last modified:
  • Reading time:10 mins read
  • Post published:

Microsoft Graph PowerShell may be quite a change compared to what you are used to due to the new set of commands and features and the processes you need to follow to ensure you are working fast and efficiently, but more importantly, securely.

Here are 10 great tips from me to ensure you work better with Microsoft Graph PowerShell!

Keep your modules up to date

This might seem like an obvious one, but it matters! Over the 6 months alone, the Microsoft Graph PowerShell modules have updated more frequently than ever! This is partly due to the nature of Microsoft Graph, where teams constantly work to improve the API and expand its reach; each SDK (Including PowerShell) is often refreshed to ensure the functionality is always available. As well as this, feedback is consistently fed back to the Microsoft Graph product managers to resolve faults and improve the product offering. 

By keeping your modules up to date, you will ensure new functionality is always available, faults are resolved quickly, and security is ensured. Quickly update your modules using the Update-Module cmdlet.

Update-Module Microsoft.Graph

Understand and create a consent process

Although the core mechanism behind Microsoft Graph PowerShell is the same as its predecessors, for admins, it should be treated differently and with care. Behind the scenes, when you run a command from a Microsoft Graph PowerShell module, a call to the Graph API is made which then interacts with the related Microsoft services, in most cases Microsoft Entra. This was also true for the legacy modules, just a different API was interacted with. 

For admins like you and me, additional security considerations need to be made with the Microsoft Graph PowerShell modules, such as considering the need for a Global Administrator or Privileged Role Administrator to consent permissions to a service principal before specific commands can be run.

For example, where previously an admin with the User Administrator role could connect to the legacy module and modify user without additional help, now, if that same User Administrator connects to Microsoft Graph PowerShell, they cannot perform any tasks without explicit consent from a Global Administrator to the necessary API permission which falls under their role (which needs to be assumed). What you don’t want is for privileged users to be assigned Global Administrator permissions out of convenience, it is up to you as the Global Administrator to ensure a convenient and efficient solution is available.

You should invest some time and effort into ensuring you have an efficient permissions consent approval flow, allowing permissions to be carefully reviewed before being assigned.

Use dedicated accounts for using Microsoft Graph PowerShell

Microsoft Graph is a powerful tool, coupled with the PowerShell SDK and overworked administrators, it can also be a dangerous tool if not properly secured.

When working with Microsoft Graph PowerShell, use a dedicated user account, then assign that account directly to the Service Principal that you are authenticating to when connecting to Microsoft Graph PowerShell. 

Assignment required for Graph applications
Assignment required for Graph applications

This step might seem overkill, but once an account in your organisation is breached, the first and fastest way for an attacker to enumerate what they can access is through code and very much likely using Microsoft Graph PowerShell.

Using a dedicated account brings a multitude of security benefits to your organisation, such as:

  • The ability to secure with Conditional Access by selecting ‘All cloud apps’ with enhanced security, such as location-based access, session policies and stronger authentication.
  • Allowing you to use a direct assignment to your service principal to easily govern who can use Microsoft Graph PowerShell. 
  • Limiting the likely-hood of breach as the account is only used for that specific task.
  • and much more!

Learn how the HTTP requests work

Every time you run a command using the Microsoft Graph PowerShell modules, an HTTP request is sent to the Graph API using either the GET, POST, PUT, PATCH or DELETE method, depending on the command you run.

To demonstrate that these HTTP requests are happening automatically, you can use the -debug parameter to show the HTTP URL that is being requested. For example, here I am running the Get-MgUser cmdlet to retrieve a list of all users in my organisation.

Get-MgUser -Debug
Get-MgUser -Debug

You can see that right away we get some debug content in the console window. Notice the Absolute Uri, this is the exact HTTP request that is being sent to Microsoft Graph. The HTTP Method also shows GET as we are retrieving or downloading information.

Here is another example where I have retrieved information for a specific user. Notice that the Absolute Uri is different compared to where I retrieved information for all users, it now shows the specific user we request in the URL. 

Get-MgUser UserId -Debug

By knowing that fundamentally each command relates to specific HTTP requests, you will have a better experience when debugging scripts, reading documentation and identifying permissions.

Your browser developer tools are your friend for figuring out the right command

Have you ever been in a situation where you need to make a change to your environment using PowerShell, but the documentation is unclear on which command to run or how to format your payload… The built-in developer tools in your favourite web browser help you solve that problem.

For example, I want to prevent standard users in my organisation from being able to join new devices to Microsoft Entra ID, but the command for the task could be one of thousands that are available to use. 

I can simplify this investigation process by making the desired change through the Microsoft Entra admin center. Then, with the network developer tools open in my browser, I can see what request is being made.

Browser developer tools for Microsoft Graph PowerShell
Browser developer tools for Microsoft Graph PowerShell

In the case of this example the following request is being made:

  • URI: https://graph.microsoft.com/beta/policies/deviceRegistrationPolicy
  • Method: PUT

Now we have this information, the fastest way to deduce the equivalent PowerShell command is use the Find-MgGraphCommand cmdlet in PowerShell:

Find-MgGraphCommand -uri https://graph.microsoft.com/beta/policies/deviceRegistrationPolicy

#or

Find-MgGraphCommand -uri /beta/policies/deviceRegistrationPolicy

Your results will look like the following:

Find-MgGraphCommand URI
Find-MgGraphCommand URI

Permissions are easy, when you know where to find them

There are certainly some additional challenges when it comes to working with Microsoft Graph PowerShell, such as the new naming convention, new and limited capabilities, and structure of responses. Still, one of the most significant learning curves is knowing which permissions you require consent before running a script.

You should learn how and where to find the correct permissions required to run your scripts, as incorrect permissions can leave your organisation open to additional risk.

Here is an example: A user needs to make ongoing changes to user properties as part of his helpdesk duties and needs to decide on which permissions suits his needs best. He runs the following command to find that information:

(Find-MgGraphCommand Update-MgUser).permissions

The output looks like the following:

Name                                         IsAdmin Description                                                       FullDescription
----                                         ------- -----------                                                       ---------------
DeviceManagementApps.ReadWrite.All           True    Read and write Microsoft Intune apps                              Allows the app to read and write the properties, group assignments and status of apps, app configurations and app protection policies managed by Microsoft Intune.
DeviceManagementConfiguration.ReadWrite.All  True    Read and write Microsoft Intune Device Configuration and Policies Allows the app to read and write properties of Microsoft Intune-managed device configuration and device compliance policies and their assignment to groups.
DeviceManagementManagedDevices.ReadWrite.All True    Read and write Microsoft Intune devices                           Allows the app to read and write the properties of devices managed by Microsoft Intune. Does not allow high impact operations such as remote wipe and password reset on the device’s owner.
DeviceManagementServiceConfig.ReadWrite.All  True    Read and write Microsoft Intune configuration                     Allows the app to read and write Microsoft Intune service properties including device enrollment and third party service connection configuration.
Directory.ReadWrite.All                      True    Read and write directory data                                     Allows the app to read and write data in your organization's directory, such as other users, groups.  It does not allow the app to delete users or groups, or reset user passwords.
User.EnableDisableAccount.All                True    Enable and disable user accounts                                  Allows the app to enable and disable users' accounts, on your behalf.
User.ManageIdentities.All                    True    Manage  user identities                                           Allows the app to read, update and delete identities that are associated with a user's account that you have access to. This controls the identities users can sign-in with.
User.ReadWrite                               False   Read and update your profile                                      Allows the app to read your profile, and discover your group membership, reports and manager. It also allows the app to update your profile information on your behalf.
User.ReadWrite.All                           False   Read and write all users' full profiles                           Allows the app to read and update user profiles without a signed in user.

While all permissions allow for at least some user properties to be updated, others also allow for more than just user properties to be modified. The correct permission to select here is User.ReadWrite.All. If the Directory.ReadWrite.All permission was selected instead, the task would then become overprivileged, which could lead to accidental loss of data or for it to be taken advantage of.

Using the Microsoft Graph Explorer web tool is a friendlier approach to reading permission. With this tool, you can search for code examples and the necessary permissions for your command. Use the Modify permissions tab to find the same information you just saw using PowerShell.

Graph Explorer permissions example
Graph Explorer permissions example

Remove dependencies from your scripts

Installing multiple or all of the Microsoft Graph PowerShell modules can be cumbersome. While you can reduce the modules you need to install by only installing the modules for the commands you use in your script, you can take this one step further… 

By replacing nearly all of the commands in your scripts with Invoke-MgGraphRequest, you can reduce the modules you need to install to just one, the Microsoft.Graph.Authentication module.

Because every cmdlet relates to a URI (or Graph URL) and the Invoke-MgGraphRequest cmdlet invokes requests using the URL, you can make any Graph request with this single cmdlet. While this process can take a little time, you both reduce the maintenance efforts for your modules and arguably the complexity of your scripts.

Check out How To Use Invoke-MgGraphRequest with PowerShell for more.

Read the Microsoft Graph Change Log

The Microsoft Graph Change log is your one-stop shop for keeping current on the changes within the Microsoft Graph API.

Naturally, not every URI, property or feature within the Graph API makes it to the PowerShell modules. Thankfully, as we saw in the previous tip, the Invoke-MgGraphRequest cmdlet can interact with these services, even if they don’t have their own dedicated cmdlet.

So, even though the Microsoft Graph Change Log is specifically for tracking changes within the API and not the PowerShell modules, it can help open doors and extend the possibilities of control using PowerShell as your tool of choice.

Don't reinvent the wheel, someone has likely done it for you

Microsoft Graph PowerShell might be new to many people, but for some of us, we have been using it daily for years. That said, there are a whole load of example scripts and solutions readily available for you to use, copy, modify, and re-purpose. Check out these resources below. They might have the perfect script for you!

Always user Filter, Never use Where!

Including the -Filter parameter with most Microsoft Graph PowerShell commands enables you to send a GET request to Microsoft Graph and filter your return responses on the server side. This means that only your filtered responses are returned to your PowerShell session, offloading the burden to Microsoft instead of processing it yourself.

The difficulty is that the -Filter syntax comes with its challenges, i.e. not knowing the supported operators or commands turns this into more of a trial-and-error game than a defined process. On the other hand, the Where statement works in the pipeline and can always be used for fine-tuning your results, but where possible, only after you have filtered your results first!

Check out my detailed post all about filtering with Microsoft Graph PowerShell: How to Use -Filter with Microsoft Graph PowerShell.

Daniel Bradley

Daniel Bradley is a UK-based technical architect and Microsoft MVP in Security. He specialises in Microsoft Entra, Intune, and Microsoft Graph PowerShell and is the author of Microsoft Graph PowerShell for Administrators. He writes and edits Our Cloud Network using experience gained from working with Microsoft cloud environments.

This Post Has 2 Comments

  1. Hugo

    Great article. What are your thoughts on using Merill Fernando’s Graph X-Ray browser extension to find the right URL?

    1. Daniel

      Hi Hugo, the Graph-X-Ray extension is a great tool! I personally use it on my corporate and personal workstation, but I also like looking at the JSON payloads through the browser dev tools too! You’ll often find some inconsistencies with the documentation and all of the available tools, where-as sometimes the simplest solution is to look at the actual calls the browser is making 🙂

Leave a Reply