Installing the Microsoft Graph Modules
Microsoft Graph Module
You need install Microsoft Graph modules only once. In the next time, run to connect to Microsoft Graph.
##Add Repopsitory
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force;
Install-PackageProvider -Name NuGet -Force;
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
##Install modules
Install-Module Microsoft.Graph -Scope CurrentUser -Force
Install-Module MSAL.PS -Scope AllUsers -Force
Sign in
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"
Get the signed-in user
Get-MgUser
List the user's joined teams
Get-MgUserJoinedTeam -UserId admin@leoguides.info
Use app-only authentication with the Microsoft Graph PowerShell
#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
5/5 - (2 votes)