{"id":8684,"date":"2025-03-28T12:38:57","date_gmt":"2025-03-28T12:38:57","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=8684"},"modified":"2025-03-28T12:38:58","modified_gmt":"2025-03-28T12:38:58","slug":"script-to-batch-enable-per-user-multifactor-authentication-in-entra-via-graph-in-powershell","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/script-to-batch-enable-per-user-multifactor-authentication-in-entra-via-graph-in-powershell","title":{"rendered":"Script to Batch enable Per-user multifactor authentication in Entra via Graph in Powershell"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code class=\"\">Install-Module -Name Microsoft.Graph -Scope CurrentUser\n\n# Connect to Microsoft Graph with required scopes\nConnect-MgGraph -Scopes \"User.Read.All\", \"Policy.ReadWrite.AuthenticationMethod\"\n\n# Get all users with UPN ending in @domain.onmicrosoft.com, with ConsistencyLevel and $count=true\n$users = Get-MgUser -Filter \"endsWith(userPrincipalName, '@domain.onmicrosoft.com')\" -All -ConsistencyLevel eventual -CountVariable userCount | Select-Object Id, UserPrincipalName\n\n# Check if users were found\nif ($users.Count -eq 0) {\n    Write-Host \"No users found with UPN ending in @doylo.onmicrosoft.com.\" -ForegroundColor Yellow\n} else {\n    Write-Host \"Found $($users.Count) users with UPN ending in @doylo.onmicrosoft.com.\" -ForegroundColor Cyan\n\n    # Loop through each user and enable MFA\n    foreach ($user in $users) {\n        $userId = $user.Id\n        $userUPN = $user.UserPrincipalName\n\n        Write-Host \"Enabling MFA for user: $userUPN\" -ForegroundColor Green\n\n        # Define the MFA state\n        $body = @{\n#Can be Enforced as well\n            \"perUserMfaState\" = \"enabled\"\n        }\n\n        # Update the user's MFA state\n        Invoke-MgGraphRequest -Method PATCH -Uri \"\/beta\/users\/$userId\/authentication\/requirements\" -Body $body\n    }\n\n    Write-Host \"MFA has been enabled for all matching users.\" -ForegroundColor Cyan\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8684","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=8684"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8684\/revisions"}],"predecessor-version":[{"id":8686,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8684\/revisions\/8686"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=8684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=8684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=8684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}