How to check if a predetermined list of users has mfa enabled via script or powershell command

Selma Saglauskas Dias Gambarini 0 Reputation points
2023-05-18T20:25:30.6833333+00:00

I need to check if users on a list have mfa enabled. There are many users to consult on the Portal. And I also don't want to do the query using the get-mfareports.ps1 script that brings the status of all users, as I would have to compare line by line with the list I already have.

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-05-18T21:26:41.1933333+00:00

    There's no need for you to compare the lists manually.

    That script is capable of creating a CSV file. Put your list in another CSV file and create a hash from whatever list is shorter. If a user is in the CSV produced by the script isn't in your list, you can write another file containing the information you need to add that user to your list.


  2. JamesTran-MSFT 36,906 Reputation points Microsoft Employee Moderator
    2023-05-18T23:04:09.19+00:00

    @Selma Saglauskas Dias Gambarini

    Thank you for your post and I apologize for the delayed response!

    From your issue, I understand that you have a pre-determined list of users, and you'd like a PowerShell script to check if the users on that list have MFA enabled. To hopefully resolve your issue or point you in the right direction, you should be able to accomplish this using the PS script(s) below.

    Note: Prior to following the below script you'll need to create a list of users that you want to check. You can do this by creating a CSV file with a column for the user's email address. For example:

    User's image


    For more info on Azure Active Directory (MSOnline):

    #Install the Azure AD MSOnline Module
    Install-Module MSOnline
    
    #Connect to your Azure AD tenant
    Connect-MsolService
    
    #Load the list of pre-defined users into PowerShell by running the following command:
    $users = Import-Csv -Path "C:\path\Desktop\users.csv"
    
    #Create an empty array to store the results:
    $results = @()
    
    #Loop through the list of users and check if MFA is enabled for each user. Instead of writing the results to the console, we'll be adding them to the $results array:
    
    foreach ($user in $users) {
    	$userObject = Get-MsolUser -UserPrincipalName $user.Email
    	$mfaStatus = $userObject.StrongAuthenticationMethods.Count
    	$result = New-Object -TypeName PSObject -Property @{
    	Email = $user.Email
    	MFAEnabled = $mfaStatus -gt 0 
    	}
    	$results += $result
    
    }
    
    #Export the $results array to a CSV file:
    $results | Export-Csv -Path "C:\path\to\results.csv" -NoTypeInformation
    

    User's image


    I hope this helps!

    If you have any other questions, please let me know. Thank you for your time and patience throughout this issue.


    If the information helped address your question, please Accept the answer. This will help us and also improve searchability for others in the community who might be researching similar information.


  3. Rich Matheisen 47,901 Reputation points
    2023-05-19T21:40:38.33+00:00

    This is a modified version of the code submitted by @JamesTran-MSFT . If it still produces incorrect results you can check if one of the StrongAuthenticationMethods has its "IsDefault" property set to $true.

    Install-Module MSOnline         # you can remove this if the module's already installed
    
    #Connect to your Azure AD tenant
    Connect-MsolService
    
    Import-Csv -Path "C:\path\Desktop\users.csv"
    
    #Loop through the list of users and check if MFA is enabled for each user. Instead of writing the results to the console, we'll create a CSV
    Import-Csv -Path "C:\path\Desktop\users.csv" |
        ForEach-Object {
            Try{
                $email = $_.Email   # used only in 'Catch' block
    	        $userObject = Get-MsolUser -UserPrincipalName $_.Email -ErrorAction STOP
                [PSCustomObject]@{
                    Email = $_.Email
                    MFAEnabled = (if ($userObject.StrongAuthenticationMethods){$true}else{$false}) 
        	    }
            }
            Catch{
                Write-Host "User '$email' was not found."
            }
    } | Export-Csv -Path "C:\path\to\results.csv" -NoTypeInformation
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.