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”

AzureAD\UserUPN

rg=AzureADVM
location=southeastasia
vmname=Win11-02
user=vmadmin
password=SETPASSWORD
image=MicrosoftWindowsDesktop:windows-11:win11-22h2-pro:22621.1105.230107
azureaduser=AZUREAD_USER_UPN
subscriptionid=azure_subcription_id
subscriptionname=azure_subscription_name


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

Processing…
Success! You're on the list.


Posted

in

, ,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

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