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 theDeviceManagementManagedDevices.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" }