Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 2 – exploring Connect-MgGraph with couple of scenarios

Hi All,
Greetings for the day!!!
Continuing on my favorite topic – Microsoft Graph Powershell
In part 1 of the series we discussed – how to start with Microsoft Graph PowerShell. If you did not got chance to look, kindly please have a look – Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 1 – starting with Microsoft Graph PowerShell – https://knowledge-junction.in/2023/08/30/microsoft-365-powershell-microsoft-graph-powershell-tutorial-part-1/
Today we will discussing one of the important CMDLET and starting point for Microsoft Graph PowerShell CMDLETs – Connect-MgGraph
Here in this article we will discuss frequently used scenarios with different parameters for Connect-MgGraph CMDLET
Connect-MgGraph
- This CMDLET is the entry point for executing Microsoft Graph PowerShell CMDLETs
- We must invoke
Connect-MgGraphbefore any commands that access Microsoft Graph - This cmdlet gets the access token using the Microsoft Authentication Library

- One of the important parameter Connect-MgGraph is scopes – delegated permissions
Example 1 – Using delegated access – specifying scopes (permissions required) – interactive way
Connect-MgGraph -Scopes "Sites.Read.All"
The above CMDLET will authenticate current user with permissions to – read all sites in tenant
- When we execute the CMDLET we will get prompt for credentials as in below snap

- As current user authenticated successfully, we get welcome message as in below snap

- We are ready to execute graph CMDLETs
Example 2 – Using delegated access – specifying scopes (permissions required) – interactive way – using device authentication
We have detailed article on DeviceLogIn – Microsoft 365 – Connecting to tenant using PnP PowerShell – Connect-PnPOnline – exploring DeviceLogIn (Device code flow) parameter- Part 2 – https://knowledge-junction.in/2021/12/18/microsoft-365-connecting-to-tenant-using-pnp-powershell-connect-pnponline-exploring-devicelogin-device-code-flow-parameter-part-2/
- Lets explore Connect-MgGraph with device authentication method
- We will use “UseDeviceAuthentication” parameter
Connect-MgGraph -Scopes "User.Read.All", "Group.ReadWrite.All" -UseDeviceAutnetication

- As in above snap we will get device code “HJUCV6QA2” and the URL – https://microsoft.com/devicelogin to login
- As we hit the URL in browser – https://microsoft.com/devicelogin , we will get prompt to enter the code as

- Enter the code and hit “Next” button
- We will get pop up for credentials and permission details – as specified in “-Scopes” parameter

- Check the URL in browser

- Once we have signed in successfully, we will get following pop-up.
- We could close the browser window

- As we successfully login – we will get welcome message as in below snap

REFERENCES
- Microsoft Graph permissions reference – All delegated access / scopes are listed here – https://learn.microsoft.com/en-us/graph/permissions-reference
- Microsoft 365 – Connecting to tenant using PnP PowerShell – Connect-PnPOnline – exploring DeviceLogIn (Device code flow) parameter- Part 2 – https://knowledge-junction.in/2021/12/18/microsoft-365-connecting-to-tenant-using-pnp-powershell-connect-pnponline-exploring-devicelogin-device-code-flow-parameter-part-2/
Thanks for reading! Stay tuned for more articles on Microsoft Graph PowerShell!
HAPPY LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂

4 Responses
[…] Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 2 – exploring Connect-MgGraph with couple of scenarios – https://microsoft365hub.in/2024/01/18/microsoft-365-powershell-microsoft-graph-powershell-tutorial-p… […]
[…] Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 2 – exploring Connect-MgGraph with couple of scenarios – https://microsoft365hub.in/2024/01/18/microsoft-365-powershell-microsoft-graph-powershell-tutorial-p… […]
[…] Once Microsoft Graph PowerShell SDK is installed, next step is to connect to Microsoft Graph using CMDLET – Connect-MgGraph with appropriate scope. We have detailed article for Connect-MgGraph – Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 2 – exploring Connect-MgGraph with couple of scenarios – https://knowledge-junction.in/2024/01/18/microsoft-365-powershell-microsoft-graph-powershell-tutori… […]
[…] We have detailed article on Connect-MgGraph CMDLET – Microsoft 365 : PowerShell – Microsoft Graph PowerShell tutorial – Part 2 – exploring Connect-… […]