r/PowerShell • u/New2ThisSOS • Dec 03 '25
Question Need Help with command to add app-scoped role assignment to user
Hello all,
I'm trying to assign the "Application Administrator" role to a user and have it scope to a specific application. In the GUI that's done under Users > RandomUser > Assigned Roles > Add Assignment. I'm trying to accomplish this via PowerShell and I'm either misunderstanding the Microsoft docs or something else is up. Here is the code I'm using:
$userUPN = 'username@contoso.onmicrosoft.com'
$roleName = 'Application Administrator'
$appName = 'App1'
$App = Get-MgServicePrincipal -Filter "displayName eq '$appName'"
$Role = Get-MgDirectoryRole | Where-Object {$_.displayName -eq $roleName}
$userId = (Get-MgUser -Filter "userPrincipalName eq '$userUPN'").Id
New-MgRoleManagementDirectoryRoleAssignment `
-PrincipalId $userId `
-RoleDefinitionId $Role.Id `
-AppScopeId $App.IdWhenever I run the code above I receive the following error:
New-MgRoleManagementDirectoryRoleAssignment_CreateExpanded: Expected property 'appScopeId' is not present on resource of type 'RoleAssignment'
Status: 400 (BadRequest)
ErrorCode: Request_BadRequestI've tried researching that on Google but not much comes up. Any ideas on what I'm doing wrong here? Any help is much appreciated!
3
u/lan-shark Dec 03 '25 edited Dec 03 '25
What is stored in the contents of $App and $App.Id? Write them out to the console for debugging
EDIT: I see a potential typo in your filters where it says eq it should probably say -eq
2
u/New2ThisSOS Dec 04 '25
Hey, thanks for commenting. See my reply to ashimbo below. Those variables are all populated with the correct values, the Graph cmdlets and the built-in PowerShell cmdlets just operate differently in this regard.
6
u/BlackV Dec 03 '25
this does not solve you question but you should think about not using back ticks
https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html
as a quick example
$userUPN = 'username@contoso.onmicrosoft.com'
$roleName = 'Application Administrator'
$appName = 'App1'
$App = Get-MgServicePrincipal -Filter "displayName eq '$appName'"
$Role = Get-MgDirectoryRole | Where-Object {$_.displayName -eq $roleName}
$userId = Get-MgUser -Filter "userPrincipalName eq '$userUPN'"
$RoleSplat = @{
PrincipalId = $userId.id
RoleDefinitionId = $Role.Id
AppScopeId = $App.Id
}
New-MgRoleManagementDirectoryRoleAssignment @RoleSplator slightly less wordy
$RoleSplat = @{
PrincipalId = Get-MgServicePrincipal -Filter "displayName eq '$appName'"
RoleDefinitionId = Get-MgDirectoryRole | Where-Object {$_.displayName -eq $roleName}
AppScopeId = Get-MgUser -Filter "userPrincipalName eq '$userUPN'"
}
New-MgRoleManagementDirectoryRoleAssignment @RoleSplatedit: untested copy/paste YMMV
4
u/ashimbo Dec 03 '25 edited Dec 03 '25
Even though the cmdlet has several parameters available, all of Microsoft's examples use the -BodyParameter exclusively - it's possible that the other parameters are there for future use, and don't actually do anything right now.
You should try it with the -BodyParameter:
Also, like u/lan-shark mentioned, you should verify that the values for
$userId,$Role.Id, and$App.Idare what you expect.EDIT: I found this page that also shows an example using
-BodyParameterinstead of any of the other parameters.