Script for Updating Device Ownership in Microsoft Graph
This script connects to Microsoft Graph with the appropriate permissions and performs the following tasks:
- Connects to Microsoft Graph: The script uses Connect-MgGraph with the DeviceManagementManagedDevices.ReadWrite.All scope to authenticate and access device management data.
- Filters Devices: Retrieves devices that are running either macOS or Windows and are owned by personal users.
- 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” }