Entra / Microsoft 365 · Users & guests
Report admin Entra ID accounts without MFA
Find Entra ID accounts with privileged roles that are not protected by MFA.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-AzureADConnect-MsolService
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
CLS$ModulesLoaded = Get-Module | Select NameIf (!($ModulesLoaded -match "AzureAD")) {Write-Host "Please connect to the Azure AD module and then restart the script" ; break}If (!($ModulesLoaded -match "MSOnline")) {Write-Host "Please connect to the Microsoft Online Services module and then restart the script"; break}# Retrieve GUIDs for the Privileged Roles (from Get-AzureADDirectoryRole)Write-Host "Finding Azure Active Directory administrative roles..."$UserAccountAdmin = Get-AzureADDirectoryRole | Where-Object {$_.DisplayName -eq ‘User Account Administrator’} | Select ObjectId$TenantAdmin = Get-AzureADDirectoryRole | Where-Object {$_.DisplayName -eq ‘Company Administrator’} | Select ObjectId$TeamsAdmin = Get-AzureADDirectoryRole | Where-Object {$_.DisplayName -eq ‘Teams Service Administrator’} | Select ObjectId$ExchangeAdmin = Get-AzureADDirectoryRole | Where-Object {$_.DisplayName -eq ‘Exchange Service Administrator’} | Select ObjectId$SharePointAdmin = Get-AzureADDirectoryRole | Where-Object {$_.DisplayName -eq ‘Sharepoint Service Administrator’} | Select ObjectId# Find out the set of accounts that hold these admin roles in the tenant$UserAccountAdmins = Get-AzureADDirectoryRoleMember -ObjectId $UserAccountAdmin.ObjectID | Select ObjectId, UserPrincipalName$TenantAdmins = Get-AzureADDirectoryRoleMember -ObjectId $TenantAdmin.ObjectID | Select ObjectId, UserPrincipalName$TeamsAdmins = Get-AzureADDirectoryRoleMember -ObjectId $TeamsAdmin.ObjectID | Select ObjectId, UserPrincipalName$ExchangeAdmins = Get-AzureADDirectoryRoleMember -ObjectId $ExchangeAdmin.ObjectID | Select ObjectId, UserPrincipalName$SharePointAdmins = Get-AzureADDirectoryRoleMember -ObjectId $SharePointAdmin.ObjectID | Select ObjectId, UserPrincipalName$MFAReport = [System.Collections.Generic.List[Object]]::new() # Create output fileWrite-Host "Finding Azure AD user accounts and checking their MFA status..."$Users = (Get-MsolUser -All | ? {$_.UserType -eq "Member" -and $_.Islicensed -eq $True} | Sort DisplayName)ForEach ($User in $Users) {$MFAMethods = $User.StrongAuthenticationMethods.MethodType$MFAEnforced = $User.StrongAuthenticationRequirements.State$DefaultMFAMethod = ($User.StrongAuthenticationMethods | ? {$_.IsDefault -eq "True"}).MethodTypeIf (($MFAEnforced -eq "Enforced") -or ($MFAEnforced -eq "Enabled")) {Switch ($DefaultMFAMethod) {"OneWaySMS" { $MethodUsed = "One-way SMS" }"TwoWayVoiceMobile" { $MethodUsed = "Phone call verification" }"PhoneAppOTP" { $MethodUsed = "Hardware token or authenticator app" }"PhoneAppNotification" { $MethodUsed = "Authenticator app" }} #End Switch}Else {$MFAEnforced= "Not Enabled"$MethodUsed = "MFA Not Used" }$MFAReportLine = [PSCustomObject] @{UserPrincipalName = $User.UserPrincipalNameDisplayName = $User.DisplayNameMFAUsed = $MFAEnforcedMFAMethod = $MethodUsedObjectId = $User.ObjectId }$MFAReport.Add($MFAReportLine)} # End For# Extract users whose accounts don't have MFA$MFAUsers = $MFAReport | ? {$_.MFAUsed -ne "Enforced"}If (!($MFAUsers)) { Write-Host "No privileged accounts found without MFA protection" ; break}Write-Host "Checking MFA status for accounts holding admin roles..."$i = 0$Report = [System.Collections.Generic.List[Object]]::new() # Create output file# Check Admin Roles if MFA not enabledForEach ($User in $MFAUsers) {$Roles = $NullIf ($UserAccountAdmins.ObjectId -Contains $User.ObjectId) {Write-Host $User.DisplayName "Account holds the User Account Admin role" -ForegroundColor Red$Roles = "Account Admin" }If ($TenantAdmins.ObjectId -Contains $User.ObjectId) {Write-Host $User.DisplayName "Account holds the Tenant Admin role" -ForegroundColor RedIf ($Roles -eq $Null) { $Roles = "Tenant Admin" } Else { $Roles = $Roles + "; Tenant Admin" } }If ($TeamsAdmins.ObjectId -Contains $User.ObjectId) {Write-Host $User.DisplayName "Account holds the Teams Admin role" -ForegroundColor RedIf ($Roles -eq $Null) { $Roles = "Teams Admin" } Else { $Roles = $Roles + "; Teams Admin" } }If ($ExchangeAdmins.ObjectId -Contains $User.ObjectId) {Write-Host $User.DisplayName "Account holds the Exchange Admin role" -ForegroundColor RedIf ($Roles -eq $Null) { $Roles = "Exchange Admin" } Else { $Roles = $Roles + "; Exchange Admin" } }If ($SharePointAdmins.ObjectId -Contains $User.ObjectId) {Write-Host $User.DisplayName "Account holds the SharePoint Admin role" -ForegroundColor RedIf ($Roles -eq $Null) { $Roles = "SharePoint Admin" } Else { $Roles = $Roles + "; SharePoint Admin" } }If ($Roles -ne $Null) {Write-Host "User" $User.DisplayName "is assigned the following roles:" $Roles -ForeGroundColor Yellow; $i++$ReportLine = [PSCustomObject]@{User = $User.DisplayNameUPN = $User.UserPrincipalNameRoles = $RolesMFA = $User.MFAUsed }$Report.Add($ReportLine) } #End if}Write-Host "All done." $i "privileged accounts found which aren't protected by MFA - see C:\temp\MFAReport.CSV for details"$Report | Out-GridView$Report | Export-CSV -NoTypeInformation C:\temp\MFAReport.CSV
Attribution
Author
Office365itpros