Automating with PowerShell: Sending MFA push messages to users

I’ve been trying to explore how Microsoft generates MFA pushes for some time now. I wanted to offer the same functionality for tools I am building. One of them being a AAD RADIUS replacement tool that’s almost done for public use, the other is a method of verifying the identity of a specific user. Unfortunately Microsoft doesn’t really publish any documentation about how they generate these tokens and send them to users.

After some reverge engineering and checking how the tokens are both received and sent on devices I’ve been able to create a PowerShell script that allows you to send MFA requests to any user that you administer either in your own tenant, or in client tenants using the secure application model. As an example I’ve included a user validation portal Azure Function, but of course you can use this in any process where you need to confirm that the user is really themselves.

Example Azure Function

Click the deploy button below to create the Azure Function that allows you send MFA requests to any user in your tenant.

After clicking the button and entering your Secure Application Model IDs and deploying the application, visiting the application will present you with this screen, enter a username there and press send and it’ll send the MFA request to a user, allowing you to validate their identity. This only works with push-based MFA types, such the PhoneApp, passwordless, or a phone call.

If you don’t feel comfortable pressing the easy deploy button, or want to check out the source you can do that over here.

Example Script

The GenPass should be a generated password. The first time you execute this for a tenant it might fail as it takes some time for the password to apply to the application in your M365 environment.

######### Secrets #########
$ApplicationId = 'ApplicationID'
$ApplicationSecret = 'ApplicationSecret'
$RefreshToken = 'RefreshToken'
$EmailToPush = ""
$Genpass = 'RandomGeneratedPasswordToSetOnMFAApplicationPleaseKeepThisSecureAsItCanBeUsedToGenerateMFARequests'
######### Secrets #########
write-host "Creating credentials and tokens." -ForegroundColor Green
$credential = New-Object System.Management.Automation.PSCredential($ApplicationId, ($ApplicationSecret | Convertto-SecureString -AsPlainText -Force))
$aadGraphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes '' -ServicePrincipal
$graphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes '' -ServicePrincipal
write-host "Connecting to MSOL" -ForegroundColor Green

Connect-MsolService -AdGraphAccessToken $aadGraphToken.AccessToken -MsGraphAccessToken $graphToken.AccessToken
$UserTenantName = $EmailToPush -split '@' | Select-Object -last 1
$UserTenantGUID = (Invoke-WebRequest "$UserTenantName/.well-known/openid-configuration" | ConvertFrom-Json).token_endpoint.Split('/')[3] 
$MFAAppID = '981f26a1-7f43-403b-a875-f8b09b8cd720'
write-host "Setting temporary password" -ForegroundColor Green
New-MsolServicePrincipalCredential -TenantId $UserTenantGUID -AppPrincipalId $MFAAppID -Type password -Usage verify -Value $GenPass -Verbose
write-host "Generating XML" -ForegroundColor Green

$XML = @"
<Lcid>en-us</Lcid><AuthenticationMethodProperties xmlns:a=""><a:KeyValueOfstringstring><a:Key>OverrideVoiceOtp</a:Key><a:Value>false</a:Value></a:KeyValueOfstringstring></AuthenticationMethodProperties><ContextId>69ff05bf-eb61-47f7-a70e-e7d77b6d47d0</ContextId>

$body = @{
    'resource'      = ''
    'client_id'     = $MFAAppID
    'client_secret' = $Genpass
    'grant_type'    = "client_credentials"
    'scope'         = "openid"

$ClientToken = Invoke-RestMethod -Method post -Uri "$UserTenantGUID/oauth2/token" -Body $body
$headers = @{ "Authorization" = "Bearer $($ClientToken.access_token)" }
write-host "Generating MFA Request" -ForegroundColor Green

$obj = Invoke-RestMethod -uri '' -Method POST -Headers $Headers -Body $XML -ContentType 'application/xml'

if($obj.BeginTwoWayAuthenticationResponse.AuthenticationResult -ne $true){
    write-host "Authentication failed, does the user have Push/Phone call MFA configured?"

With this you could implement Azure AD based MFA in your own applications, I have some other blogs incoming that will show you how to combine this with an Azure Function to create a RADIUS alternative that supports AAD, and some other cool gizmos. As always, Happy PowerShelling.


  1. Jarad Olson May 28, 2021 at 4:23 pm

    Hi! This is so cool! Thanks for posting the code! You mentioned that you’re almost ready to release your AAD RADIUS alternative, and I am extremely interested in trying it / contributing somehow. Please let me know where you plan to publish the code so I can get started right away. Thanks!

  2. Nicholas Bennett June 25, 2021 at 10:34 pm

    Does this require a partner account? i have tried to run it on my single tenant and i get errors when trying to setup the secure app model

      1. ian July 28, 2021 at 7:08 pm

        Should the powershell script work in a single tenant? or could it be modified?

  3. Kent August 12, 2021 at 7:33 am

    Where can I learn more about your AAD Radius replacement? Been looking into this for a while, was going to do some POC with Freeradius but haven’t had time

  4. jason August 19, 2021 at 6:05 am

    If the user doesn’t have the Microsoft authenticator app, can this fall back to SMS, if a cell was put into the end user profile?

Leave a Reply to Nicholas Bennett Cancel 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.