Azure – PowerShell – Update Device Registered User

email me

In Azure, you have the ability to update the registered user of a device. This is how you do it using PowerShell.

# MrNetTek
# eddiejackson.net/blog
# 1/17/2020
# free for public use 
# free to claim as your own

clear-host

$user = 'username@domain.com'
$computer = 'computername'

$userSearch = $user.SubString(0,1)
$newUser = Get-AzureADUser -SearchString $userSearch | where { $_.UserPrincipalName -eq $user } | Select -ExpandProperty ObjectId
$device = Get-AzureADDevice | where { $_.DisplayName -eq $computer }
$oldOwner = (Get-AzureADDeviceRegisteredOwner -ObjectId $device.ObjectId).ObjectId

Add-AzureADDeviceRegisteredOwner -ObjectId $device.ObjectId -RefObjectId $newUser
Remove-AzureADDeviceRegisteredOwner -ObjectId $device.ObjectId -RefObjectId $oldOwner
Get-AzureADDeviceRegisteredOwner -ObjectId $device.ObjectId

 

Notes

Get-AzureADUser
Get-AzureADDevice
Get-AzureADDeviceRegisteredOwner
Add-AzureADDeviceRegisteredOwner
Remove-AzureADDeviceRegisteredOwner
Get-AzureADDeviceRegisteredOwner
Substring method

 

Further Testing

clear-host
$computer = 'Lab-PC1'
$oldUser = 'User1'
$newUser = 'User2'

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

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"

#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

 

tags: PowerShell, Azure registered user, change registered user in Azure Intune, MrNetTek