web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / I am trying to automat...
Power Apps
Suggested Answer

I am trying to automate the process of adding an existing user to an Environment and add roles

(1) ShareShare
ReportReport
Posted on by 2
Here is my manual workflow that I want to script. After a new user is created, I head over to PowerApps Admin Center and select an Environment. Next, I click add user and select the newly created user. I then assign them 5 roles. I am struggling with the code. I was tryng top use the module below but I am striking out. Any advice would be appreciated. Thank you.
Microsoft.PowerApps.Administration.PowerShell
I have the same question (0)
  • Michael E. Gernaey Profile Picture
    53,362 Super User 2025 Season 2 on at
     
    You said here is my flow, but I do not see a flow.
     
     
  • Suggested answer
    Solutionm8 Profile Picture
    92 on at
    Hi

    Looking into the documentation of the Microsoft.PowerApps.Administration.PowerShell module it doesn't look like it contains a command for assigning security roles in environments with a dataverse instance. Only environments without.

    Maybe a combination of the Microsoft.Xrm.Data.PowerShell and the Microsoft.PowerApps.Administration.PowerShell can do the job? Maybe something like this:
    # Install the necessary modules if not already installed
    Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Scope CurrentUser
    Install-Module -Name Microsoft.Xrm.Data.PowerShell -Scope CurrentUser
    
    # Import the modules
    Import-Module Microsoft.PowerApps.Administration.PowerShell
    Import-Module Microsoft.Xrm.Data.PowerShell
    
    # Define the environment URL and user details
    $environmentUrl = "https://your_environment.crm.dynamics.com"
    $environmentId = "your_environment_id"
    $userEmail = "user@example.com"
    $securityRoles = @("Environment Admin", "Environment Maker") # Replace with actual role names
    
    # Authenticate to Power Platform
    Add-PowerAppsAccount
    
    # Add the user to the environment
    New-AdminPowerAppEnvironmentUser -EnvironmentName $environmentId -UserPrincipalName $userEmail
    
    # Authenticate to the Dataverse environment
    $connection = Get-CrmConnection -InteractiveMode -ServerUrl $environmentUrl
    
    # Get the user's system user ID
    $userId = (Get-CrmRecords -conn $connection -EntityLogicalName systemuser -FilterAttribute domainname -FilterOperator eq -FilterValue $userEmail).CrmRecords[0].systemuserid
    
    # Assign security roles to the user
    foreach ($roleName in $securityRoles) {
        $roleId = (Get-CrmRecords -conn $connection -EntityLogicalName role -FilterAttribute name -FilterOperator eq -FilterValue $roleName).CrmRecords[0].roleid
        New-CrmRecord -conn $connection -EntityLogicalName systemuserroles -Fields @{ systemuserid = $userId; roleid = $roleId }
    }
    
    Write-Host "User added and roles assigned successfully."
    Another option might be to use license to role mapping: https://learn.microsoft.com/en-us/power-platform/admin/assign-security-roles#license-to-role-mapping

    If you don't need the security role to be directly assigned, you could also use Azure AD security groups and add them as teams in the Dataverse environment and assign the teams roles.

    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    Visit my YouTube channel: Solutionm8

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard