Skip to content

Set-MgmtGraphUser

MgmtGraph: Updates Microsoft Graph user account properties

#Requires -Version 5.1
#Requires -Modules Microsoft.Graph.Users

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$Identity,

    [string]$DisplayName,

    [string]$JobTitle,

    [string]$Department,

    [bool]$AccountEnabled
)

Process {
    try {
        $params = @{
            'UserId'      = $Identity
            'Confirm'     = $false
            'ErrorAction' = 'Stop'
        }

        if ($PSBoundParameters.ContainsKey('DisplayName')) { $params.Add('DisplayName', $DisplayName) }
        if ($PSBoundParameters.ContainsKey('JobTitle')) { $params.Add('JobTitle', $JobTitle) }
        if ($PSBoundParameters.ContainsKey('Department')) { $params.Add('Department', $Department) }
        if ($PSBoundParameters.ContainsKey('AccountEnabled')) { $params.Add('AccountEnabled', $AccountEnabled) }

        if ($params.Count -gt 3) {
            Update-MgUser @params
            
            $result = [PSCustomObject]@{
                Identity  = $Identity
                Action    = "UserUpdated"
                Status    = "Success"
                Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            }
            Write-Output $result
        }
        else {
            Write-Warning "No properties specified to update for user '$Identity'."
        }
    }
    catch {
        throw
    }
}

Specifies the UserPrincipalName or ID of the user to update.

Optional. Specifies the new display name.

Optional. Specifies the new job title.

Optional. Specifies the new department.

Optional. Specifies whether the account is enabled ($true) or disabled ($false).

An interactive directory of PowerShell scripts.