Script for Updating Device Ownership in Microsoft Graph

1 min. readlast update: 09.21.2024

This script connects to Microsoft Graph with the appropriate permissions and performs the following tasks:

  1. Connects to Microsoft Graph: The script uses Connect-MgGraph with the DeviceManagementManagedDevices.ReadWrite.All scope to authenticate and access device management data.
  2. Filters Devices: Retrieves devices that are running either macOS or Windows and are owned by personal users.
  3. Updates Device Ownership: Loops through the filtered devices and updates the ownership type from "personal" to "company" using the Update-MgDeviceManagementManagedDevice cmdlet.

Here is the Script:

Connect-MgGraph -Scopes "DeviceManagementManagedDevices.ReadWrite.All"
 Get-MgDeviceManagementManagedDevice |
 Where-Object {($_.OperatingSystem -EQ "macOS") -or ($_.OperatingSystem -EQ "Windows")}|
 Where-Object ManagedDeviceOwnerType -EQ "personal" |
 ForEach-Object {Update-MgDeviceManagementManagedDevice -ManagedDeviceId $_.Id -ManagedDeviceOwnerType "company" } 

Was this article helpful?