Functions/Get-UserMFARegistration.ps1


function Get-UserMFARegistration {

    [CmdletBinding()]
    param (
        [Parameter()] [string] $UserPrincipalName
    )

    if ($UserPrincipalName) {
        Get-MgBetaUser -UserId $UserPrincipalName
    } else {
        $mgUsers = Get-MgBetaUser -All -Filter "usertype eq 'member'" -Property SignInActivity
    }

    if (!($mgUsers)) {
        return "No users found"
    }

    $result = @()

    $mgUsers | ForEach-Object {
        $obj = [PSCustomObject]@{
            Id                               = $_.Id
            Userprincipalname                = $_.UserPrincipalName
            DisplayName                      = $_.DisplayName
            Country                          = $_.Country
            Enabled                          = $_.AccountEnabled
            LastSignInDateTime               = $_.SignInActivity.LastSignInDateTime
            LastNonInteractiveSignInDateTime = $_.SignInActivity.LastNonInteractiveSignInDateTime
            AuthenticationMethods            = Get-MgBetaUserAuthenticationMethod -UserId $_.Id | Select-Object -ExpandProperty AdditionalProperties | Where-Object "@odata.type" -NE "#microsoft.graph.passwordAuthenticationMethod" | Out-String
        }
        $result += $obj
    }

    return $result


}