Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 1

fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell - executing "Connect-MgGraph" CMDLET - connected to MSGraph
fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell - executing "Connect-MgGraph" CMDLET - connected to MSGraph

Hi All,

Greetings for the day!!! Today starting new series on my favourite topics – Microsoft Graph + PowerShell

This series will be very interesting, stay tuned going to cover lots of Microsoft Graph and PowerShell features 🙂

Take away from this article

  • What is Microsoft Graph PowerShell
  • What are the benefits of Microsoft Graph PowerShell
  • How to install the Microsoft Graph PowerShell SDK
  • Demo – Using our first Microsoft Graph PowerShell SDK
  • Further REFERENCES

What is Microsoft Graph PowerShell and Features

  • Microsoft Graph PowerShell is based on Microsoft Graph API
  • The Microsoft Graph PowerShell SDK is an API wrapper for the Microsoft Graph APIs
  • The Microsoft Graph PowerShell SDK exposes all the Microsoft Graph APIs to use in PowerShell
  • The Microsoft Graph PowerShell SDK will help to manage every Azure AD feature that has API in Microsoft Graph
  • The Microsoft Graph PowerShell is the replacement for the Azure AD PowerShell and MSOnline modules
  • The Microsoft Graph PowerShell is now recommended for interacting with Azure AD
  • Microsoft Graph PowerShell works with PowerShell 7 and later
  • Microsoft Graph PowerShell also compatible with Windows PowerShell 5.1.
  • Cross-platform support : Microsoft Graph PowerShell works on all platforms including Windows, macOS, and Linux
  • Supports modern authentication: Microsoft Graph PowerShell supports the Microsoft Authentication Library (MSAL) which offers more security. For example, we can use passwordless sign-in experiences
  • Receives regular updates: Microsoft Graph PowerShell commands are updated regularly to support the latest Graph API updates.

How to install the Microsoft Graph PowerShell SDK

fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell SDK
fig : Microsoft 365 – PowerShell – Microsoft Graph PowerShell SDK

  • Open the Windows PowerShell ISE and copy the given command
fig : Windows PowerShell ISE
fig : Windows PowerShell ISE – opening
  • Once Windows PowerShell ISE opened – execute the command to install – Microsoft Graph PowerShell SDK
  • We will us Install-Module command
Microsoft 365 - PowerShell - Installing Microsoft Graph PowerShell SDK - "Untrusted repository" dialog
fig : Microsoft 365 – PowerShell – Installing Microsoft Graph PowerShell SDK – “Untrusted repository” dialog

  • For time being click on “Yes to All” button
  • As we click on “Yes to All” button, installation begins as shown in below fig

fig : Microsoft 365 - PowerShell - Installing Microsoft Graph PowerShell SDK - Installation Progress
fig : Microsoft 365 – PowerShell – Installing Microsoft Graph PowerShell SDK – Installation Progress
  • On successful completion it shows “Completed...” message as
fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell SDK installed successfully
fig : Microsoft 365 – PowerShell – Microsoft Graph PowerShell SDK installed successfully
  • As “Microsoft Graph PowerShell SDK” installed successfully we are ready to go to use the Microsoft Graph PowerShell CMDLETS

Prerequisites to use Microsoft Graph PowerShell

  • Microsoft Graph is a protected web API for accessing data in Microsoft cloud services like Azure Active Directory and Microsoft 365.
  • It’s protected by the Microsoft identity platform, which uses OAuth access tokens to verify that an app /user is authorized to call Microsoft Graph.
  • Microsoft Graph APIs can be accessed in one of two ways
    • Delegated access, an app acting on behalf of a signed-in user.
    • App-only access, an app acting with its own identity.
fig : Microsoft 365 - Permissions require for Microsoft Graph APIs - Delegated permissions and Application permissions
fig : Microsoft 365 – Permissions require for Microsoft Graph APIs – Delegated permissions and Application permissions

  • Here we are using Delegated access as we are executing PowerShell CMDLETs with currently signed user
  • Delegated access requires delegated permissions, also referred to as scopes.
    • Scopes are permissions that are exposed by a given resource and they represent the operations that an app can perform on behalf of a user.
    • For example – to list all the users in our Microsoft 365 tenant, one of the permission we need like “User.Read.All”
fig : Microsoft 365 - Permissions require for Microsoft Graph APIs
fig : Microsoft 365 – Permissions require for Microsoft Graph APIs

fig : Microsoft 365 - Permissions require for Microsoft Graph APIs
fig : Microsoft 365 – Permissions require for Microsoft Graph APIs

  • We will use “Connect-MgGraph” CMDLET with required respective scopes to authenticate as

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

  • This CMDLET will prompt for the credentials as

fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell - executing "Connect-MgGraph" CMDLET
fig : Microsoft 365 – PowerShell – Microsoft Graph PowerShell – executing “Connect-MgGraph” CMDLET
  • As we put the credentials successfully, we get the dialog “Sign in to your account” as in below figure
  • We need to “Accept” the permissions on this dialog and proceed to continue

fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell - executing "Connect-MgGraph" CMDLET - "Sign in to your account" dialog
fig : Microsoft 365 – PowerShell – Microsoft Graph PowerShell – executing “Connect-MgGraph” CMDLET – “Sign in to your account” dialog
  • As we click on “Accept“, and it went successfully we have following message in console / In PowerShell

PS C:\> D:\PS\MSGRAPH.ps1
Welcome to Microsoft Graph!

Connected via delegated access using 14d82eec-204b-4c2f-b7e8-296a70dab67e
Readme: https://aka.ms/graph/sdk/powershell
SDK Docs: https://aka.ms/graph/sdk/powershell/docs
API Docs: https://aka.ms/graph/docs

NOTE: You can use the -NoWelcome parameter to suppress this message.

fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell - executing "Connect-MgGraph" CMDLET - connected to MSGraph
fig : Microsoft 365 – PowerShell – Microsoft Graph PowerShell – executing “Connect-MgGraph” CMDLET – connected to MSGraph
  • As we connected/authenticated successfully to Microsoft Graph we are ready to execute the Graph PowerShell CMDLETs

Demo – Using our first Microsoft Graph PowerShell CMDLET – getting list of all users in our Microsoft 365 tenant

  • In this demo we will simply explore the CMDLET – Get-MgUser
  • Authenticate the current user using – Connect-MgGraph
  • Once we authenticated successfully we are ready to execute – Get-MgUser as

PS C:\> Get-MgUser

DisplayName                       Id                                   Mail                                                     UserPrincipalName                        
-----------                       --                                   ----                                                     -----------------                        
Conference room booking           a2196299-4a89-4303-91cd-f28b5fc6a664 Conferenceroombooking@knowledgejunction1.onmicrosoft.com Conferenceroombooking@knowledgejunctio...
Garima Sabadra                    7822f8c8-052e-4a7f-a23f-548309928065 garimasabadra@knowledgejunction1.onmicrosoft.com         garimasabadra@knowledgejunction1.onmic...
Shweta Parakh                     89bc78c7-58e0-482d-8951-f97a23d701a9 KnowledgeJunction@knowledgejunction1.onmicrosoft.com     KnowledgeJunction@knowledgejunction1.o...
KnowledgeJunction                 3656fd61-42e2-440b-9258-f8792aab6e2f KnowledgeJunctionSM@knowledgejunction1.onmicrosoft.com   KnowledgeJunctionSM@knowledgejunction1...
Nilesh                            eda687fc-b2d3-41cf-a74d-3157dec10272 nilesh@knowledgejunction1.onmicrosoft.com                nilesh@knowledgejunction1.onmicrosoft.com
Prasham Sabadra                   ff594132-b1a6-4bd3-92e5-1d4621ca9180 prasham@knowledgejunction1.onmicrosoft.com               prasham@knowledgejunction1.onmicrosoft...
Prasham1 Sabadra                  ac650a5f-cf73-4581-abb1-5851eaaf31ab prasham1@knowledgejunction1.onmicrosoft.com              prasham1@knowledgejunction1.onmicrosof...
Prasham Sabadra                   7fe8666c-cb94-4998-a1f3-c8c0f4aacdfb psabadra@gmail.com                                       psabadra_gmail.com#EXT#@knowledgejunct...
Pune meeting room (YSPOSITEADMIN) d91ed747-28ca-48d3-a2fe-dfc4ea9ae99f punemeetingroom@knowledgejunction1.onmicrosoft.com       punemeetingroom@knowledgejunction1.onm...
Sayyam Sabadra                    fb2bbf5b-1629-4e1c-a37d-8e2b3c81c137 sayyam@knowledgejunction1.onmicrosoft.com                sayyam@knowledgejunction1.onmicrosoft.com
Ravindra                          4e8ca8f1-45b4-483a-a5d6-a4ac304e88bb seconduser@knowledgejunction1.onmicrosoft.com            seconduser@knowledgejunction1.onmicros...
Prashant Jain                     aba827c5-9554-46d4-8e8f-05078b308c34 thirduser@knowledgejunction1.onmicrosoft.com             thirduser@knowledgejunction1.onmicroso...

fig : Microsoft 365 - PowerShell - Microsoft Graph PowerShell - executing "Connect-MgUser" CMDLET
fig : Microsoft 365 – PowerShell – Microsoft Graph PowerShell – executing “Connect-MgUser” CMDLET

REFERENCES

Thanks for reading! If you think this is useful, kindly please like and share! HAVE a FANTASTIC LEARNING ! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: