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!
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.3
u/lan-shark Dec 03 '25 edited Dec 03 '25
What is stored in the contents of
$Appand$App.Id? Write them out to the console for debuggingEDIT: I see a potential typo in your filters where it says
eqit should probably say-eq6
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
or slightly less wordy
edit: untested copy/paste YMMV