Microsoft Graph PowerShell commands for reviewing Microsoft Entra ID groups, directory roles, privileged identities, owners, and MFA registration.
This project is the Microsoft Graph successor to the original AzureAD/MSOnline-based AzurePowerCommands.ps1 script.
The examples in this README assume that the updated script is named
MgPowerCommands.ps1.
- Recursively enumerate users, groups, and service principals in nested groups.
- Recursively resolve members assigned to Microsoft Entra directory roles.
- Identify members of selected privileged roles.
- Generate role overviews including users, groups, service principals, and owners.
- Build a consolidated list of highly privileged identities.
- Report MFA registration, capability, registered methods, preferred methods, and legacy per-user MFA state.
- Return normal PowerShell objects that can be filtered, exported, and processed further.
- Windows PowerShell 5.1 or PowerShell 7.
- The Microsoft Graph PowerShell SDK.
- A Microsoft Entra work or school account.
- Administrative consent for the required Microsoft Graph delegated permissions.
- A supported Microsoft Entra directory role when an API requires both Graph permissions and directory RBAC permissions.
The legacy AzureAD, AzureADPreview, and MSOnline modules are not required.
Install the Microsoft Graph PowerShell SDK:
Install-Module Microsoft.Graph -Scope CurrentUserDownload and import MgPowerCommands.ps1:
Import-Module .\MgPowerCommands.ps1 -ForceThe script can also be dot-sourced:
. .\MgPowerCommands.ps1Confirm that the public commands are available:
Get-Command -Name Get-Mg* -CommandType Function | Where-Object Source -Like '*MgPowerCommands*'The following delegated scopes cover all functionality in the script:
$Scopes = @(
'Directory.Read.All'
'AuditLog.Read.All'
'UserAuthenticationMethod.Read.All'
'Policy.Read.All'
)
Connect-MgGraph -Scopes $ScopesThe scopes are used as follows:
| Scope | Used for |
|---|---|
Directory.Read.All |
Users, groups, service principals, owners, directory roles, and role members |
AuditLog.Read.All |
MFA registration information from userRegistrationDetails |
UserAuthenticationMethod.Read.All |
Detailed authentication method objects with -Detailed |
Policy.Read.All |
Legacy per-user MFA state from the Microsoft Graph beta authentication requirements endpoint |
Inspect the current Graph session with:
Get-MgContextDisconnect when finished:
Disconnect-MgGraphRecursively enumerates a Microsoft Entra group and nested groups. By default, the command returns unique user objects.
Get-MgGroup -GroupId '<GROUP-ID>' |
Get-MgGroupMemberRecursiveFind a group by display name:
Get-MgGroup -All |
Where-Object DisplayName -eq '<GROUP-NAME>' |
Get-MgGroupMemberRecursiveGet-MgGroup -GroupId '<GROUP-ID>' |
Get-MgGroupMemberRecursive -ReturnGroupsGet-MgGroup -GroupId '<GROUP-ID>' |
Get-MgGroupMemberRecursive -ReturnServicePrincipalsReturned objects include properties such as:
ObjectType
ObjectId
DisplayName
UserPrincipalName
AppId
AccountEnabled
Mail
SecurityEnabled
IsAssignableToRole
Recursively enumerates the direct and nested members of an active Microsoft Entra directory role. By default, the command returns unique user objects.
Get-MgDirectoryRole -DirectoryRoleId '<ROLE-ID>' |
Get-MgDirectoryRoleMemberRecursiveFind a role by display name:
Get-MgDirectoryRole -All |
Where-Object DisplayName -eq 'Global Administrator' |
Get-MgDirectoryRoleMemberRecursiveGet-MgDirectoryRole -DirectoryRoleId '<ROLE-ID>' |
Get-MgDirectoryRoleMemberRecursive -ReturnGroupsGet-MgDirectoryRole -DirectoryRoleId '<ROLE-ID>' |
Get-MgDirectoryRoleMemberRecursive -ReturnServicePrincipalsEnumerates members of the privileged role set defined in the script.
Get-MgPrivilegedRolesMembersGet-MgPrivilegedRolesMembers |
Format-Table ObjectType, DisplayName, UserPrincipalName, ObjectId -AutoSizeGet-MgPrivilegedRolesMembers -ReturnGroupsGet-MgPrivilegedRolesMembers -ReturnServicePrincipalsGet-MgPrivilegedRolesMembers |
Export-Csv .\PrivilegedUsers.csv -NoTypeInformation -Encoding UTF8Generates an overview of the selected privileged Microsoft Entra roles. The output includes counts and names for users, groups, group owners, service principals, and service principal owners.
$MgPrivilegedRolesOverview = Get-MgPrivilegedRolesOverview
$MgPrivilegedRolesOverview | Format-Table -AutoSizeShow all properties without table truncation:
$MgPrivilegedRolesOverview |
Format-List Role, UserCount, Users, GroupCount, Groups,
GroupOwners, SPsCount, SPs, SPsOwnersExport the overview:
$MgPrivilegedRolesOverview |
Export-Csv .\PrivilegedRolesOverview.csv -NoTypeInformation -Encoding UTF8The returned properties are:
Role
UserCount
Users
GroupCount
Groups
GroupOwners
SPsCount
SPs
SPsOwners
Store the original objects in the variable first. Do not assign the result of
Format-Tableto the variable when the data will be filtered or exported later.
Generates the same type of overview for all active Microsoft Entra directory roles returned by Get-MgDirectoryRole.
$MgDirectoryRoleOverview = Get-MgDirectoryRoleOverview
$MgDirectoryRoleOverview | Format-Table -AutoSizeShow all properties:
$MgDirectoryRoleOverview |
Format-List Role, UserCount, Users, GroupCount, Groups,
GroupOwners, SPsCount, SPs, SPsOwnersExport the overview:
$MgDirectoryRoleOverview |
Export-Csv .\DirectoryRoleOverview.csv -NoTypeInformation -Encoding UTF8Get-MgPrivilegedRolesOverviewChecks only the selected privileged roles defined in the script.
Get-MgDirectoryRoleOverviewChecks every active directory role returned by Microsoft Graph.
Returns a consolidated and deduplicated collection of highly privileged users and service principals, including owners of privileged groups and service principals.
$PrivilegedObjects = Get-MgPrivilegedObjects
$PrivilegedObjects | Format-Table -AutoSizeFilter the returned objects by type:
$PrivilegedUsers = $PrivilegedObjects |
Where-Object ObjectType -eq 'User'
$PrivilegedServicePrincipals = $PrivilegedObjects |
Where-Object ObjectType -eq 'ServicePrincipal'Export all privileged identities:
$PrivilegedObjects |
Export-Csv .\PrivilegedObjects.csv -NoTypeInformation -Encoding UTF8Reports MFA registration and authentication method information for users.
The primary MFA registration values come from the Microsoft Graph userRegistrationDetails report:
MFA Configuredmaps toisMfaRegistered.MFA Capablemaps toisMfaCapable.MFA Methodscontains registered methods that can represent strong authentication.Registered Methodsalso includes methods that can be used only for SSPR, such as email.Per-User MFAis the legacy per-user MFA state and does not represent Conditional Access enforcement.
Get-MgUser -All |
Get-MgUserMFAConfigurationGet-MgUser -UserId 'user@contoso.com' |
Get-MgUserMFAConfigurationGet-MgUser -UserId 'user@contoso.com' |
Get-MgUserMFAConfiguration -Detailed |
Format-ListDetailed output can include:
Authentication Method Objects
MobilePhoneRegistered
OneWaySMS
TwoWayVoiceMobile
PhoneAppOTP
PhoneAppNotification
Registered Email
Registered Phone
FIDO2
WindowsHelloForBusiness
TemporaryAccessPass
Get-MgPrivilegedRolesMembers |
Get-MgUserMFAConfigurationGet-MgPrivilegedRolesMembers |
Get-MgUserMFAConfiguration -Detailed |
Format-ListGet-MgUser -All |
Get-MgUserMFAConfiguration |
Export-Csv .\MFAConfiguration.csv -NoTypeInformation -Encoding UTF8
MFA Configuredmeans that a user has registered for MFA. It does not prove that MFA is required for every sign-in. MFA enforcement can come from Conditional Access, Security Defaults, Identity Protection, or legacy per-user MFA.
| Previous command | Microsoft Graph command |
|---|---|
Get-AzureADGroupMemberRecursive |
Get-MgGroupMemberRecursive |
Get-AzureADDirectoryRoleMemberRecursive |
Get-MgDirectoryRoleMemberRecursive |
Get-AzureADPrivilegedRolesMembers |
Get-MgPrivilegedRolesMembers |
Get-AzureADPrivilegedRolesOverview |
Get-MgPrivilegedRolesOverview |
Get-AzureADDirectoryRoleOverview |
Get-MgDirectoryRoleOverview |
Get-AzureADPrivilegedObjects |
Get-MgPrivilegedObjects |
Get-AzureADUserMFAConfiguration |
Get-MgUserMFAConfiguration |
The input commands also changed:
| Previous command | Microsoft Graph equivalent |
|---|---|
Connect-AzureAD |
Connect-MgGraph |
Get-AzureADUser |
Get-MgUser |
Get-AzureADGroup |
Get-MgGroup |
Get-AzureADDirectoryRole |
Get-MgDirectoryRole |
Get-MsolUser |
Get-MgUser and the Microsoft Graph authentication reporting APIs |
Common parameter changes include:
-ObjectId -> -UserId, -GroupId, -DirectoryRoleId, or -ServicePrincipalId
-All $true -> -All
Get-MgDirectoryRole returns directory roles that are activated in the tenant. Get-MgDirectoryRoleOverview therefore does not list inactive role templates.
The current commands enumerate active directory role membership. Eligible Microsoft Entra Privileged Identity Management assignments are not included.
Get-MgPrivilegedRolesMembers and Get-MgPrivilegedRolesOverview use a role-name list defined in the script. Review this list when Microsoft adds or renames directory roles, or when a tenant treats additional roles as privileged.
The script uses Microsoft Graph beta endpoints internally for selected compatibility and reporting operations, including the legacy per-user MFA state. Beta APIs can change and should be retested after Microsoft Graph updates.
Microsoft Graph might not return owners for some Exchange-created groups, distribution groups, or groups synchronized from an on-premises environment.
Microsoft Graph permissions alone might not be sufficient for all directory data. In delegated sessions, the signed-in user can also require an appropriate Microsoft Entra directory role. Insufficient access can result in objects containing only an ID and object type.
Files downloaded from the internet can be marked as blocked by Windows:
Unblock-File .\MgPowerCommands.ps1
Import-Module .\MgPowerCommands.ps1 -ForceIf an organizational policy enforces AllSigned, the script must be signed by a trusted code-signing certificate.
Check the effective execution policies:
Get-ExecutionPolicy -ListReconnect with the required scopes and confirm that admin consent has been granted:
Disconnect-MgGraph
Connect-MgGraph -Scopes @(
'Directory.Read.All'
'AuditLog.Read.All'
'UserAuthenticationMethod.Read.All'
'Policy.Read.All'
)Then inspect the session:
Get-MgContext | Format-ListRemove-Module MgPowerCommands -ErrorAction SilentlyContinue
Import-Module .\MgPowerCommands.ps1 -ForceAlternatively, open a new PowerShell session.
Get-MgPrivilegedRolesOverview -Verbose- Install the Microsoft Graph PowerShell SDK
- Connect-MgGraph
- Upgrade from Azure AD PowerShell to Microsoft Graph PowerShell
- Get-MgDirectoryRole
- Get-MgUserAuthenticationMethod
- Get-MgReportAuthenticationMethodUserRegistrationDetail
- userRegistrationDetails resource
Jony Schats - 0xJs
This project is licensed under the GNU General Public License v3.0.