Privileged access management is one of the most critical components of Microsoft Entra ID security. Permanently assigning highly privileged roles like Global Administrator increases the attack surface and violates Zero Trust security principles.
Microsoft Entra Privileged Identity Management (PIM) helps organizations reduce standing administrator privileges by enabling just-in-time privileged access. Instead of granting permanent admin access, users can be made “eligible” for privileged roles and activate them only when required.
Using Microsoft Graph PowerShell, administrators can automate the bulk onboarding of users into PIM eligible assignments — eliminating repetitive manual effort while improving security governance and compliance.
This approach is especially useful for:
In this article, we’ll walk through a production-ready Graph PowerShell script that:
Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.
A PIM eligible assignment allows users to activate privileged roles only when required, instead of permanently holding the role.
This significantly reduces the risk of credential compromise and privilege abuse.
| Assignment Type | Description | Security Impact |
|---|---|---|
| Active Assignment | User permanently holds the role | Higher risk |
| Eligible Assignment | User activates role only when needed | Lower risk |
| Time-Bound Active Assignment | Temporary active access | Moderate risk |
| Just-In-Time Access | Requires activation approval | Strong Zero Trust model |
Microsoft recommends using eligible assignments whenever possible for privileged roles like Global Administrator.
Before running the script:
Install Microsoft Graph PowerShell module if needed:
Install-Module Microsoft.Graph -Scope CurrentUser
Microsoft Entra Privileged Identity Management requires one of the following licenses:
Without appropriate licensing, PIM role assignments will fail.
Connect to Microsoft Graph using the following scopes:
Connect-MgGraph -Scopes `
"RoleManagement.ReadWrite.Directory",
"Directory.ReadWrite.All",
"User.Read.All"
| Permission | Purpose |
|---|---|
| RoleManagement.ReadWrite.Directory | Manage PIM role assignments |
| Directory.ReadWrite.All | Access directory objects |
| User.Read.All | Validate user accounts |
The signed-in account should hold one of the following roles:
CSV File Format
Prepare a CSV file named:
C:\Temp\PIMEligibleAdmins.csv
Sample CSV Structure
UserPrincipalName
admin1@contoso.com
admin2@contoso.com
admin3@contoso.com
# Connect to Microsoft Graph
Connect-MgGraph -Scopes `
"RoleManagement.ReadWrite.Directory",
"Directory.ReadWrite.All",
"User.Read.All"
# Import CSV
$Users = Import-Csv "C:\Temp\PIMEligibleAdmins.csv"
# Initialize results array
$Results = @()
# Get Global Administrator role definition
$RoleDefinition = Get-MgRoleManagementDirectoryRoleDefinition `
-Filter "displayName eq 'Global Administrator'"
if (-not $RoleDefinition) {
Write-Host "Global Administrator role definition not found. Exiting script." -ForegroundColor Red
return
}
# Schedule information
$StartDateTime = Get-Date
$EndDateTime = $StartDateTime.AddYears(1)
foreach ($UserEntry in $Users) {
$UPN = $UserEntry.UserPrincipalName
Write-Host "`nProcessing user: $UPN" -ForegroundColor Cyan
try {
# Validate user
$User = Get-MgUser -UserId $UPN -ErrorAction SilentlyContinue
if (-not $User) {
Write-Host "User not found: $UPN" -ForegroundColor Red
$Results += [PSCustomObject]@{
UserPrincipalName = $UPN
Status = "Failed"
Message = "User not found"
TimeStamp = Get-Date
}
continue
}
# Check for existing eligible assignment
$ExistingAssignment = Get-MgRoleManagementDirectoryRoleEligibilityScheduleInstance `
-Filter "principalId eq '$($User.Id)'"
$AlreadyAssigned = $ExistingAssignment | Where-Object {
$_.RoleDefinitionId -eq $RoleDefinition.Id
}
if ($AlreadyAssigned) {
Write-Host "Eligible assignment already exists for $UPN" -ForegroundColor Yellow
$Results += [PSCustomObject]@{
UserPrincipalName = $UPN
Status = "Skipped"
Message = "Eligible assignment already exists"
TimeStamp = Get-Date
}
continue
}
# Create eligible assignment request
$Params = @{
Action = "adminAssign"
PrincipalId = $User.Id
RoleDefinitionId = $RoleDefinition.Id
DirectoryScopeId = "/"
Justification = "Bulk PIM onboarding using Graph PowerShell"
ScheduleInfo = @{
StartDateTime = $StartDateTime
Expiration = @{
Type = "afterDateTime"
EndDateTime = $EndDateTime
}
}
}
New-MgRoleManagementDirectoryRoleEligibilityScheduleRequest `
-BodyParameter $Params
Write-Host "PIM eligible assignment created for $UPN" -ForegroundColor Green
$Results += [PSCustomObject]@{
UserPrincipalName = $UPN
Status = "Success"
Message = "Eligible assignment created successfully"
TimeStamp = Get-Date
}
}
catch {
Write-Host "Error processing user: $UPN" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
$Results += [PSCustomObject]@{
UserPrincipalName = $UPN
Status = "Failed"
Message = $_.Exception.Message
TimeStamp = Get-Date
}
}
}
# Export final results
$Results | Export-Csv `
"C:\Temp\PIMEligibleAssignmentResults.csv" `
-NoTypeInformation
Write-Host "`nResults exported to C:\Temp\PIMEligibleAssignmentResults.csv" `
-ForegroundColor Green
The script first connects to Microsoft Graph using the required scopes for Privileged Identity Management (PIM), directory access, and user validation.
Connect-MgGraph -Scopes `
"RoleManagement.ReadWrite.Directory","Directory.ReadWrite.All",
"User.Read.All"
These permissions are necessary to:
The script imports all target users from the CSV file.
$Users = Import-Csv "C:\Temp\PIMEligibleAdmins.csv"
Each row in the CSV should contain a valid UserPrincipalName.
This allows administrators to bulk onboard multiple users into PIM eligible Global Administrator assignments without manual intervention.
Instead of using transcript logging, the script creates a structured results array to track:
$Results = @()
This makes the script more enterprise-friendly because the results can later be exported into a CSV report for auditing and troubleshooting.
The script retrieves the Microsoft Entra role definition for the Global Administrator role.
$RoleDefinition = Get-MgRoleManagementDirectoryRoleDefinition `
-Filter "displayName eq 'Global Administrator'"
If the role definition cannot be found, the script exits safely to avoid invalid assignment requests.
The script defines:
$StartDateTime = Get-Date
$EndDateTime = $StartDateTime.AddYears(1)
In this example, users remain eligible for Global Administrator access for one year.
You can customize this duration as needed for your organization’s security policies.
Before creating a PIM assignment, the script verifies whether the user exists in Microsoft Entra ID.
$User = Get-MgUser -UserId $UPN -ErrorAction SilentlyContinue
This prevents:
If the user does not exist, the script records the failure in the results array and continues processing the next user.
The script verifies whether the user already has a PIM eligible Global Administrator assignment.
$ExistingAssignment = Get-MgRoleManagementDirectoryRoleEligibilityScheduleInstance `
-Filter "principalId eq '$($User.Id)'"
It then checks whether the assignment already exists for the Global Administrator role.
This prevents:
Users who already have eligible assignments are marked as “Skipped” in the final CSV report.
The script creates a new eligible assignment request using:
New-MgRoleManagementDirectoryRoleEligibilityScheduleRequest
The request includes:
The assignment type used is:
Action = "adminAssign"
This makes the user eligible for Global Administrator access through Microsoft Entra PIM.
For every processed user, the script records:
Example success entry:
$Results += [PSCustomObject]@{
UserPrincipalName = $UPN
Status = "Success"
Message = "Eligible assignment created successfully"
TimeStamp = Get-Date
}
This provides structured reporting for administrators.
After processing all users, the script exports the results to:
C:\Temp\PIMEligibleAssignmentResults.csv
using:
$Results | Export-Csv `
"C:\Temp\PIMEligibleAssignmentResults.csv" `
-NoTypeInformation
The CSV report includes:
This makes the script suitable for:
Get-MgRoleManagementDirectoryRoleEligibilityScheduleInstance
Get-MgRoleManagementDirectoryRoleEligibilityScheduleInstance |
Where-Object {
$_.RoleDefinitionId -eq $RoleDefinition.Id
}
Get-MgRoleManagementDirectoryRoleAssignmentScheduleInstance
| Use Case | Benefit |
|---|---|
| Bulk onboarding security admins | Faster privileged access setup |
| Temporary project-based admin access | Reduced standing privileges |
| Global admin reduction initiatives | Lower attack surface |
| Compliance-driven governance | Improved auditing |
| Helpdesk privilege elevation | Controlled escalation |
| Tiered administration | Better privilege separation |
To improve privileged access security:
Organizations often combine role-assignable groups with PIM eligible assignments to simplify privileged access management at scale.
| Error | Cause | Solution |
|---|---|---|
| Insufficient privileges to complete the operation | The signed-in account lacks required Graph permissions or Entra roles. | Ensure:
|
| Resource not found | The user specified in the CSV file does not exist. | Verify:
|
| The role assignment already exists | The user already has an eligible assignment. | The updated script already prevents duplicate assignments by validating existing eligibility before assignment. |
| License assignment required | Microsoft Entra ID P2 licensing is unavailable. | Assign: Microsoft Entra ID P2 OR Microsoft 365 E5 licensing |
You can further enhance this solution by adding:
Yes. PIM requires Microsoft Entra ID P2 or Microsoft 365 E5 licensing.
Eligible assignments require role activation before access is granted, whereas active assignments provide standing administrator access.
Yes. Microsoft Graph PowerShell supports automation of eligible and active role assignments.
Eligible assignments require the user to activate the role through Microsoft Entra PIM.
Yes, but Microsoft strongly recommends using eligible assignments instead of permanent standing access.
Microsoft Entra PIM plays a critical role in modern Zero Trust privileged access strategies by eliminating unnecessary standing administrator privileges.
Using Microsoft Graph PowerShell, administrators can automate privileged access onboarding at scale while improving operational efficiency, security governance, and compliance readiness.
The enhanced script covered in this article includes:
This makes it suitable for enterprise-grade privileged access management and large-scale Microsoft Entra administration.
© Created and Maintained by LEARNIT WELL SOLUTIONS. All Rights Reserved.