Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Azure and Microsoft 365 / Using Microsoft Graph API to Access Azure via PowerShell

March 15, 2024 Azure and Microsoft 365PowerShell

Using Microsoft Graph API to Access Azure via PowerShell

Microsoft Graph API allows you to access any objects in the Azure AD (Microsoft 365) tenant using a single REST API point (https://graph.microsoft.com). You are likely to think that it may be interesting to web developers only, but it is quite the other way round. Some data, objects, or resource properties in Microsoft 365 can only be accessed through Microsoft Graph. To collect analytic data, statistics, or other information, an Azure administrator has to use Microsoft Graph.

In this article, we’ll show you how to register your app in Azure AD, get an authentication token, connect to different Microsoft 365 resources (Azure AD, Office 365, Intune, SharePoint, Teams, OneNote, etc.) using RESTful and PowerShell Invoke-RestMethod cmdlet. You can use Microsoft Graph both to get data and manage objects in Azure.

Contents:
  • Registering Microsoft Graph Application on Azure AD
  • Connecting to Azure Microsoft Graph API Using PowerShell

Registering Microsoft Graph Application on Azure AD

To access resources in your Azure tenant using Microsoft Graph, you need to create a new Azure AD app and allow it to access different Azure objects.

  1. Sign-in to the Azure portal https://portal.azure.com/
  2. Go to Azure Active Directory -> App registration;
  3. Create a new app (New registration);
    register new app using azure app registration
  4. Enter the name of your app: azGraphPowerShellApp, select who can use the app: Accounts in this organizational directory only (tenantname only - Single tenant) and click Register;
    register application in Microsoft Azure
  5. Then select what Azure resources your application is allowed to access. Go to the API permissions section;
  6. By default, an app is allowed to read data about a current AzureAD user only (User.Read). We will grant it read permissions on all properties of Microsoft 365 users and groups;
  7. Click Add a permission, select Microsoft Graph;
  8. There are two basic permission types in Microsoft Graph (Delegated permission – when something is done on behalf of a user who runs an app and Application Permission – when an app is called by an external script). Select Application Permission;
  9. In the list that appears, you can select what permissions you will assign to your application to access Azure resources and objects. In my example, I have added Group -> Group.Read.All, GroupMember -> GroupMember.Read.All, User -> User.Read.All (if you want your app to read any data in your tenant, select Directory.Read.All);
    granting Azure resource permissions for the app
  10. Click Grant admin consent to grant access on behalf of the administrator.
    view assigned permissions for azure application

To authenticate in an app, you can use a certificate or a secret. A secret is an automatically generated password. The username is an app ID. Let’s create a secret for your app.

  1. Open Certificates & secrets -> New client secrets;
  2. Enter the key name and set its validity time(I have specified 12 months);
    create a secret for azure application
  3. Copy the value from the Value field (it is the password for the app). Save the password in the Azure Key Vault or in your password manager, since after you exit the app, the password value will be hidden (you will have to create the secret again);
    azure client secret
  4. Then copy your app ID (Application client ID) and Azure tenant ID (Directory tenant ID).

azure application credentials

Paste your values to PowerShell variables:

$ApplicationID = "1111111-1111-1111-1111-11111111111"
$TenatDomainName = "2222222-2222-2222-2222-222222222222"
$AccessSecret = "3333333333333333333333333333333333333333333"

Connecting to Azure Microsoft Graph API Using PowerShell

To use Microsoft Graph API from PowerShell, you don’t need to install any separate PowerShell modules (like Azure AD). You can interact with it using a built-in Invoke-RestMethod cmdlet.

To connect to Graph API, you must get an access token. The following PowerShell script allows you to authenticate in your app and get a Microsoft Graph API access token.

In this example we are using a secret (a password) as plain text in the script. In real life, it is not recommended to do it. You should better request a secret interactively or extract it from a secret vault. Also, take care of your secrets if you store your PowerShell scripts on Git.

$ApplicationID = "1111111-1111-1111-1111-11111111111"
$TenatDomainName = "2222222-2222-2222-2222-222222222222"
$AccessSecret = "3333333333333333333333333333333333333333333"
$Body = @{
Grant_Type    = "client_credentials"
Scope         = "https://graph.microsoft.com/.default"
client_Id     = $ApplicationID
Client_Secret = $AccessSecret
}
$ConnectGraph = Invoke-RestMethod -Uri https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token -Method POST -Body $Body
$token = $ConnectGraph.access_token

powershell - getting Azure Graph token using Invoke-RestMethod

Using the token, you can run different queries against your Azure tenant using GraphAPI.

For example, the script below displays a list of groups in your Azure AD:

$GrapGroupUrl = 'https://graph.microsoft.com/v1.0/Groups/'
(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $GrapGroupUrl -Method Get).value.displayName

get azure groups via graph api

You can display the date when an Azure AD group was created:

$GrapGroupUrl = 'https://graph.microsoft.com/v1.0/Groups/'
$Groups=(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $GrapGroupUrl -Method Get).value
$Groups | select displayName,createdDateTime

To show a user name, UPN, and email address:

$GrapUserUrl = 'https://graph.microsoft.com/v1.0/users'
$users=(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $GrapUserUrl -Method Get).value
$users | select displayName,userprincipalname,mail

list Azure AD users via Microsoft Graph API

If you haven’t granted permissions to access Azure AD objects to your app, the following error will appear when trying to run Invoke-RestMethod:

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

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

In the examples above, we only read data from Azure AD using the GET method. But you can also use POST, PUT, PATCH, or DELETE methods to make changes. For instance, you can create a user in Azure AD, reset a password, change a description, etc.

To view available Microsoft Graph API properties or methods in your browser, you can use Graph Explorer (https://developer.microsoft.com/en-us/graph/graph-explorer).

exploring Azure AD Objects with MIcrosoft Graph Explorer

Microsoft also has a special Microsoft Graph PowerShell SDK for interacting with Microsoft Graph (Install-Module Microsoft.Graph). But we showed that you can access Microsoft Graph directly from PowerShell.
1 comment
4
Facebook Twitter Google + Pinterest
previous post
Get-ADUser: Find Active Directory User Info with PowerShell
next post
How to Convert SID to User/Group Name and User to SID

Related Reading

View Windows Update History with PowerShell (CMD)

April 30, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

How to Write Logs to the Windows Event...

March 3, 2025

1 comment

Alex Dryer July 25, 2023 - 9:21 pm

Hello! Thank you for this article, it made it easy to set up the API in Azure. You referenced this guide in a previous article about checking read/unread email status in Exchange here: https://woshub.com/check-read-unread-email-status-exchange/

I did some tinkering around in MS Graph after authenticating and was able to find the isRead information in one of the GET queries, but was only able to run it for the messages in my own inbox. I also wasn’t able to figure out how to specify which email ID I wanted to run the query on. Is this a limitation of the API or is there something I’m missing? I’d much appreciate a follow up to that article if its possible to use Graph for the same purpose as you outlined with the Get-MessageTrackingLog command in PowerShell.

Thank you!

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • Checking User Sign-in Logs in Entra ID (Microsoft 365)
  • How to Manually Configure Exchange or Microsoft 365 Account in Outlook 365/2019/2016
  • Search and Delete Emails from User Mailboxes on Exchange Server (Microsoft 365) with PowerShell
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Blank Sign-in Screen in Office 365 Apps (Outlook, Teams, etc.)
  • Removing Built-in Teams Chat in Windows 11
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top