PowerShell – Microsoft Graph API

Need to connect to Microsoft Graph with PowerShell? The following PowerShell script does just that (pretty easily)!

So what is Microsoft Graph API?

“Microsoft Graph is a Microsoft developer platform that connects multiple services and devices. Initially released 2015, the Microsoft Graph builds on Office 365 APIs and allows developers to integrate their services with Microsoft products, including Windows, Office 365, and Azure. At its Build 2017 conference, Microsoft announced it would use the Microsoft Graph to bring new functionality and connectivity between Windows and other OS platforms, including Android and iOS.”Wikipedia

The following script have a function called “Get-GraphAPIAccessToken” (line 127 in the code below) which have two optional options available.

  • Credential
    • If you don’t want to get an login prompt, when connecting to the API. Then use:
      -Credential (Create-PSCredential -Username “admin@contoso.onmicrosoft.com” -Password “AdminPasswordHere”)
  • ClientID
    • If you need to use a registered application in Azure AD, specify the application id. Otherwise it will use a well-known value that are registered with Azure AD. An example would be:
      -ClientID 3b7e46ca-4495-410f-9691-f90793f0f666

 

There are some requirements before you can run the code, which is:

  • You need an valid internet connection (duuuh?!).
  • The script leverages AzureRM and the script will try to install it, but if it fails you can use “Install-Module AzureRM -SkipPublisherCheck -AllowClobber -Force -Confirm:$false;” to install it.

When you have an access token, you can use the following example which gets the current user profile details:

Get the Azure AD users:

Send an e-mail (need an Exchange license on the user):

 

For more usage examples, Microsoft have create an repository on GitHub.

Alex is a infrastructure consultant at Mansoft A/S. I have a Data Technician degree from Denmark, Copenhagen, where I also live and work. My goal is to be a MVP in digital technology industry. While I'm still in my younger years, I already have over 10+ years of experience in the field. I focuses on Microsoft technologies but I'm not limited to only that. I also does a lot of work in the Unix and Linux world.

Leave a Reply

Your email address will not be published. Required fields are marked *

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