Azure – PowerShell – Device User and Owner ObjectId

email me

Return the User objectId and Device objectId to be able to make changes to the device record in Azure.

# MrNetTek
# 6/10/2020
# free for public use
# free to claim as your own


# Set your main variables
$computer = 'Lab-PC1'
$oldUser = 'User1'
$newUser = 'User2'
# Collect your data
Get-AzureADDevice -SearchString "$computer"
$devObjID = Get-AzureADDevice -SearchString $computer | Select -ExpandProperty objectid
$devName = Get-AzureADDevice -SearchString $computer | Select -ExpandProperty DisplayName
$curOwnerName = Get-AzureADDeviceRegisteredOwner -ObjectId $devObjID | Select -ExpandProperty UserPrincipalName
$curUserName = Get-AzureADDeviceRegisteredUser -ObjectId $devObjID | Select -ExpandProperty UserPrincipalName
$oldUserObj = AzureADUser -SearchString $oldUser | Select -ExpandProperty objectid
$newUserObj = AzureADUser -SearchString $newUser | Select -ExpandProperty objectid
# Display what you found
Write-Host "DisplayName: $devName"
Write-Host "Current Owner: $curOwnerName"
Write-Host "Current User: $curUserName"
Write-Host "Device ObjectId: $devObjID"
Write-Host "`n"
Write-Host "Old User ObjectId: $oldUserObj"
Write-Host "New User ObjectId: $newUserObj"
Write-Host "`n`n"
# Make changes here
# Add-AzureADDeviceRegisteredUser -ObjectId $devObjID -RefObjectId $newUserObj #ADUser
# Add-AzureADDeviceRegisteredOwner -ObjectId $devObjID -RefObjectId $newUserObj #ADUser
# Remove-AzureADDeviceRegisteredOwner -ObjectId $devObjID -OwnerId $oldUserObj
# Remove-AzureADDeviceRegisteredUser -ObjectId $devObjID -OwnerId $oldUserObj

# Verify changes
# Get-AzureADUserOwnedDevice -ObjectId $newUserObj