Powershell script to extract Azure PIM roles eligible and assigned roles/groups assignment

MrFlinstone 706 Reputation points
2025-03-02T22:30:57.2366667+00:00

Within Azure PIM, when I export assignment via the Azure PIM blade, using the export functionality I noticed that it is missing some roles and doesn't give the full picture, I am wondering if its possible to write a powershell script that will export out ALL assignments within PIM, eligible, permanent assignments, groups etc.

Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2025-03-07T07:59:09.4266667+00:00

    Hello @MrFlinstone,

    In addition to Vasil Michev's response, you can also make use of below Microsoft Graph PowerShell script to export the Azure PIM roles eligible and assigned roles/groups assignment:

    
    # Install-Module Microsoft.Graph.Identity.Governance
    
    # Import-Module Microsoft.Graph.Identity.Governance
    
    Connect-MgGraph -Scopes RoleManagement.Read.Directory, Directory.Read.All
    
    $EligiblePIMRoles = Get-MgRoleManagementDirectoryRoleEligibilitySchedule -All -ExpandProperty *
    
    $AssignedPIMRoles = Get-MgRoleManagementDirectoryRoleAssignmentSchedule -All -ExpandProperty *
    
    $PIMRoles = $EligiblePIMRoles + $AssignedPIMRoles
    
    $Report = [System.Collections.Generic.List[Object]]::new()
    
    foreach ($a in $PIMRoles) {
    
        $regex = "^([^.]+)\.([^.]+)\.(.+)$"
    
        $a.Principal.AdditionalProperties.'@odata.type' -match $regex | out-null
    
        $obj = [pscustomobject][ordered]@{
    
            Assigned                 = $a.Principal.AdditionalProperties.displayName
    
            "Assigned Type"          = $matches[3]
    
            "Assigned Role"          = $a.RoleDefinition.DisplayName
    
            "Assigned Role Scope"    = $a.directoryScopeId
    
            "Assignment Type"        = (&{if ($a.AssignmentType -eq "Assigned") {"Active"} else {"Eligible"}})
    
            "Is Built In"            = $a.roleDefinition.isBuiltIn
    
            "Created Date"           = $a.CreatedDateTime
    
            "Expiration type"        = $a.ScheduleInfo.Expiration.type
    
            "Expiration Date"        = switch ($a.ScheduleInfo.Expiration.EndDateTime) {
    
                {$a.ScheduleInfo.Expiration.EndDateTime -match '20'} {$a.ScheduleInfo.Expiration.EndDateTime}
    
                {$a.ScheduleInfo.Expiration.EndDateTime -notmatch '20'} {"N/A"}
    
            }
    
        }
    
        $report.Add($obj)
    
    }
    
    $Report | Export-CSV -path C:\temp\AllPIMRolesExport.csv -NoTypeInformation
    
    

    enter image description here

    All the PIM roles exported successfully in the CSV like below:

    enter image description here

    Please do not forget to click  "Accept the answer” and  Yes  wherever the information provided helps you, this can be beneficial to other community members.

    User's image

    If you have any other questions or still running into more issues, let me know in the  "comments"  and I would be happy to help you.

    1 person found this answer helpful.

  2. Loïc MICHEL 5 Reputation points Microsoft Employee
    2025-08-08T21:49:31.35+00:00

    Please check out my module EasyPIM which includes a comprehensive set of commands for managing PIM across Entra, Azure and Groups! https://github.com/kayasax/EasyPIM

    Example:
    User's image

    1 person found this answer helpful.

  3. Vasil Michev 123K Reputation points MVP Volunteer Moderator
    2025-03-03T16:57:25.59+00:00

Your answer

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