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.

Experienced advanced operations engineer with a demonstrated history of working in the information technology and services industry. Skilled in Windows Server, Azure, Ethical Hacking, Office 365, Exchange, Jenkins, SCCM, Octopus Deploy and PowerShell to name a few. Strong engineering professional with a big passion for knowledge.

4 Comments

  1. Michael

    Hi Alex

    Thanks for the very helpful post. Do you know if it is possible to send and get emailmessages without registering an application?

    I can get messages in the graph explorer but not in my script.

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.