Disclaimer: This article is for less technical people who just want to do a few easy queries of Microsoft 365. It is not for people who enjoy programming and want to know all there is about PowerShell and Microsoft Graph.
Microsoft has been pushing Microsoft Graph for data connectivity, so here is a simplified set of steps for those who just want to do some basic queries. I’ve always been mystified by the complexity and flexibility of using PowerShell to query Microsoft 365 data. Not everyone is a programmer or even wants to be, but it is a helpful tool.
Step 1: Install Powershell 7+. There are at least 5 ways to install Powershell, but you can ignore that article and just click this installation link to install version 7.4.5.
Step 2: Run PowerShell and execute the command ‘Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser’. This step is optional for this exercise, but you may need to use it later for added security during downloads and script execution.
Step 3: Execute the command ‘Install-Module Microsoft.Graph’. Answer Y to the prompt and be patient. This will take several minutes.
Step 4: Execute the command ‘Connect-MgGraph -Scopes “Group.ReadWrite.All”,”User.Read.All”’. If you are not already signed into your Microsoft 365 account, you will be prompted to authenticate. You may need admin permission for some queries.
Step 5: Test it by executing the following example commands:
Get-MgUser. You should see a list of all your Microsoft 365 users.
Get-MgUser | Select DisplayName,Mail
Get-MgUser -Filter “startsWith(DisplayName,’Tom’)”
Get-MgUser > output.txt
Get-MgGroup. List of all your groups.
That’s it. After you’ve run through these steps once, you don’t need to repeat Steps 1 and 3 unless you use a different computer.
Now that you are connected, delve a little deeper if you want. As you think of new queries you will be drawn deeper into the language and its nuances. Additional modules will likely need to be installed.
If you have existing PowerShell scripts that need migrating, here is a side-by-side comparison of the Azure AD and MSOnline cmdlets in Microsoft Graph.
TIP: In PowerShell, execute the command ‘$Profile’ and create the file Microsoft.PowerShell_profile.ps1 in that folder path. The commands you put in this file will run every time you start PowerShell. For example, put your Connect-MgGraph (Step 4) command in this file and it will automatically connect every time you run PowerShell.

Leave a comment