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

How To Connect To Microsoft Graph API Using PowerShell

October 20, 2023
in Blog, Microsoft Graph
0
ADVERTISEMENT

Table of Contents

Installing the Microsoft Graph Modules

Note Note: You need install Microsoft Graph modules only once. In the next time, run Connect-MgGraph to connect to Microsoft Graph.

1️⃣ Open Windows PowerShell as administrator then run the following commands:

Pw3
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
Install-PackageProvider -Name NuGet -Force
Install-Module PowerShellGet -Force
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -Force

Connecting to Microsoft Graph using Windows PowerShell

Use the Connect-MgGraph command to sign in with the required scopes. You’ll need to sign in with an admin account to consent to the required scopes.

Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All"
Bg1268

Note: Finding the right scope can be a bit challenging at the beginning. You can visit this link to get more information about scopes.

You can now use the Graph API. When you are working with Graph in PowerShell you can add additional scopes to your session by simply using the Connect-MsGraph command again with the new permissions.

Get the users

PS C:\> Get-MgUser

Id                                   DisplayName Mail                           UserPrincipalName              UserType
--                                   ----------- ----                           -----------------              --------
7e5ba72f-5647-4ffe-b589-56ade1ab649a Bon Ben     [email protected] [email protected]
389b11ee-b7ec-426d-b37d-87d8650c354b Anna        [email protected]              [email protected]
259f3044-e23d-42f6-bf75-b9df229ec542 Chris       [email protected]             [email protected]
cc98d39e-0e27-4c62-878e-42e49f1c70d4 David       [email protected]             [email protected]
75dc1c12-a6bb-4bc9-b138-69df5926131f Maria       [email protected]             [email protected]
6ce1ac93-d56c-439b-8f29-f99b3b36aea5 May         [email protected]               [email protected]
5c24d3db-60ba-4585-bcb8-7494402bcca4 Tom         [email protected]               [email protected]
258a6c86-503c-4f41-98fc-cb8a4e6bd0f3 Tonny       [email protected]             [email protected]

List the user’s joined teams

PS C:\> Get-MgUserJoinedTeam -UserId [email protected]

Id                                   DisplayName Description IsArchived
--                                   ----------- ----------- ----------
7fa305ba-61ef-4340-9d71-0ff9fa79192f Bon Ben     Bon Ben     False
1d60a3e4-04ad-4631-bcca-4e5c8d3a6a81 M365G       M365G       False

Use app-only authentication with the Microsoft Graph PowerShell

Register an app in Entra ID: https://aad.portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps

#Generate Access Token to use in the connection string to MSGraph
$AppId = '90cb4cab-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$TenantId = '95cb1f18-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$ClientSecret = 'app registration secret'
 
Import-Module MSAL.PS
$MsalToken = Get-MsalToken -TenantId $TenantId -ClientId $AppId -ClientSecret ($ClientSecret | ConvertTo-SecureString -AsPlainText -Force)
 
#Connect to Graph using access token
Connect-Graph -AccessToken $MsalToken.AccessToken

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Download and Install Microsoft Graph Module in PowerShell

Next Post

How to Fix Auto Login Checkbox is Missing in Windows 10

Related Posts

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
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 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