Ms Graph - Disable User Account

Son 316 Reputation points
2022-07-06T10:16:51.793+00:00

Hi,

I am writing a PowerShell script to disable Azure AD users that are not synced from on-premises.

As part of the script I am running a foreach loop to disable the users with the following command:

$params = @{  
    AccountEnabled = "false"  
            }  

Update-MgUser -UserId $User.Id -BodyParameter $params  

This worked for a load of guest users but it did not work for accounts which have admins roles assigned to them. I am getting the following error for these accounts:

"Update-MgUser : Insufficient privileges to complete the operation.
At line:3 char:9

  • Update-MgUser -UserId $User.Id -BodyParameter $params
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidOperation: ({ UserId = 59c8...softGraphUser }:<>f__AnonymousType552) [Update-MgUser_Update1], RestException1
  • FullyQualifiedErrorId : Authorization_RequestDenied,Microsoft.Graph.PowerShell.Cmdlets.UpdateMgUser_Update1"

The connection into MS Graph is using an App Registration and Certificate for authentication. The app permissions included are the following:

218142-api-permissions.png

Hopefully someone can help in locating the actual permissions required.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,885 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,609 questions
{count} votes

Accepted answer
  1. Vasil Michev 112.4K Reputation points MVP
    2022-07-06T11:04:12.307+00:00

    To update the accountEnabled property of admin users, you need to use delegate permissions. It's not possible to do it via application permissions. See for example this GitHub issue: https://github.com/microsoftgraph/microsoft-graph-docs/issues/14731


2 additional answers

Sort by: Most helpful
  1. MegaMunch 10 Reputation points
    2024-10-17T03:29:19.6966667+00:00

    If anyone comes across this in the future, you need to include the scope "User.EnableDisableAccount.All" when starting a new session.

    1 person found this answer helpful.
    0 comments No comments

  2. Limitless Technology 39,791 Reputation points
    2022-07-07T07:30:50.507+00:00

    Hello Son-3712,

    Since the question is related to scripting of Microsoft Graph I would recommend you to get in touch with the specific community for that product at:

    https:// techcommunity.microsoft.com/t5/microsoft-graph/bd-p/OfficeGraph

    This way, the community will be more experience and able to provide expert advice of your question.

    ------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--


Your answer

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