Skip to content

Set-MgmtGraphDevice

MgmtGraph: Updates Microsoft Graph device properties

#Requires -Version 5.1
#Requires -Modules Microsoft.Graph.Identity.DirectoryManagement

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

    [string]$DisplayName,

    [bool]$AccountEnabled,

    [bool]$IsCompliant,

    [string]$OS,

    [string]$OSVersion
)

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

        if ($PSBoundParameters.ContainsKey('DisplayName')) { $params.Add('DisplayName', $DisplayName) }
        if ($PSBoundParameters.ContainsKey('AccountEnabled')) { $params.Add('AccountEnabled', $AccountEnabled) }
        if ($PSBoundParameters.ContainsKey('IsCompliant')) { $params.Add('IsCompliant', $IsCompliant) }
        if ($PSBoundParameters.ContainsKey('OS')) { $params.Add('OperatingSystem', $OS) }
        if ($PSBoundParameters.ContainsKey('OSVersion')) { $params.Add('OperatingSystemVersion', $OSVersion) }

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

Specifies the ID of the device to update.

Optional. Specifies the new display name for the device.

Optional. Specifies whether the device account is enabled.

Optional. Specifies whether the device complies with MDM policies.

Optional. Specifies the operating system name.

Optional. Specifies the operating system version.

An interactive directory of PowerShell scripts.