Azure 1 min read

How to Join a VM to a Domain by PowerShell in Azure

Michael Wu ·

When transitioning from VMware PowerCLI to Azure, I discovered that older approaches using Set-AzVMExtension produced an error about multiple VMExtensions per handler not being supported.

Solution: Set-AzVMADDomainExtension

The recommended approach uses a newer cmdlet from the Az module.

Steps

  1. Run a PowerShell session
  2. Connect to Azure subscription using Connect-AzAccount
  3. Execute the following script:
$DomainName = "your_domain_name"
$VMName = "your_VM_Name"
$credential = Get-Credential your_Domain_account
$ResourceGroupName = "your_RG_name"

Set-AzVMADDomainExtension -DomainName $DomainName `
    -VMName $VMName `
    -Credential $credential `
    -ResourceGroupName $ResourceGroupName `
    -JoinOption 0x00000001 `
    -Restart -Verbose

Critical Parameter

The -JoinOption parameter requires 0x00000001 to join a domain. Without specifying this value, the computer joins a workgroup instead. This aligns with the Windows API fJoinOptions settings.

The key is the -JoinOption parameter — make sure to include it.