Create Windows 11 VM and Join it to Azure AD Using Azure CLI

In this blog post, we’ll walk you through the process of creating a Windows 11 Virtual Machine and Joining it to Azure AD using Azure CLI using a single script.

In today’s world, with the rise of remote work, having the ability to create and join a Windows 11 virtual machine (VM) to Azure AD is crucial. Fortunately, it’s possible to do this quickly and easily using Azure CLI.

Azure CLI Script

The full script to create a Windows 11 machine and join it to Azure AD is shown below. make sure you set the variables at the top of the script with your subscription details and password for the local account.

The machine will be joined to Azure AD automatically, and to log in, you will need to use the following login prefix”



az account set --subscription $subscriptionname
az group create --name $rg --location $location

az vm create \
    --resource-group $rg \
    --name $vmname \
    --image $image  \
    --assign-identity \
    --admin-username $user \
    --admin-password $password

az vm extension set \
    --publisher Microsoft.Azure.ActiveDirectory \
    --name AADLoginForWindows \
    --resource-group $rg \
    --vm-name $vmname

az role assignment create \
    --role "Virtual Machine Administrator Login" \
    --assignee $azureaduser \
    --scope "/subscriptions/$subscriptionid/resourceGroups/$rg"

To deploy the VM, save it as .azcli if you have the Azure CLI Tools installed and set the permissions on the script to execute chmod +x script.azcli

Success! You're on the list.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.