How to Create an Azure Virtual Machine with PowerShell.
Creating Azure virtual machines through the portal is straightforward, but automation with PowerShell gives you repeatability, consistency, and the ability to integrate VM creation into your broader infrastructure workflows. In this guide, I’ll walk you through creating a Windows VM using PowerShell, explaining each step so you can adapt the script for your own needs.
Prerequisites
Before starting, ensure you have:
- Azure PowerShell module installed
- An active Azure subscription
- Sufficient permissions to create resources in your subscription
Getting Started
First, fire up a PowerShell console and login to your subscription:
1Connect-AzAccount # Modern replacement for Login-AzureRmAccount
Step-by-Step VM Creation
1. Create a Resource Group
Resource groups serve as logical containers for Azure resources, making management and organization easier.
1$resourceGroup = "test-infra"
2$location = "North Europe"
3
4New-AzResourceGroup -Name $resourceGroup -Location $location
Tip: Choose a location closest to your users for better performance.
2. Create Storage Account
Your VM’s virtual hard disks need somewhere to be stored. Storage account names must be globally unique across all of Azure.
1$storageAccountName = "teststorage1x2" # Must be globally unique
2New-AzStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup -SkuName Standard_LRS -Location $location
Note: For production environments, consider Premium storage for better performance.
3. Create Virtual Network
VMs need a network environment to communicate with each other and the outside world.
1$vnetName = "test-net"
2$subnet = New-AzVirtualNetworkSubnetConfig -Name frontendSubnet -AddressPrefix 10.0.1.0/24
3$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet
Tip: Plan your network address space carefully, especially if you’ll connect to on-premises networks.
4. Setup IP Address and Network Interface
Configure how your VM will connect to networks and the internet.
1$nicName = "testvm-nic"
2$pip = New-AzPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
3$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
Security note: For production VMs, consider whether you need a public IP at all, and implement Network Security Groups.
5. Start Virtual Machine Configuration
Define the core settings for your VM, including its name and size.
1$vmName = "testvm1"
2$vm = New-AzVMConfig -VMName $vmName -VMSize "Standard_B1s" # Updated to modern size
Tip: Use
Get-AzVMSize -Location $locationto see available VM sizes in your region.
6. Set Virtual Machine Credentials
Create secure login credentials for your VM.
1$cred = Get-Credential -Message "Enter admin credentials for the VM" # Interactive prompt (recommended)
2
3# Alternative approach (not recommended for scripts that will be committed to source control):
4# $username = "YOURUSERNAME"
5# $password = ConvertTo-SecureString "YOUR_PASSWORD" -AsPlainText -Force
6# $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
Security best practice: Never hardcode passwords in scripts. Use Azure Key Vault or other secure methods for production deployments.
7. Select Operating System for Virtual Machine
Choose which OS image to use from the Azure Marketplace.
1$vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
2$vm = Set-AzVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2019-Datacenter" -Version "latest"
Note: You can run
Get-AzVMImageSku -Location $location -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer"to see available Windows Server versions.
8. Add Network Interface to Virtual Machine
Connect your VM to the network interface created earlier.
1$vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id
9. Create Disk for Virtual Machine
Configure the OS disk for your VM.
1$diskName = "os-disk"
2$storageAcc = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
3$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $diskName + ".vhd"
4$vm = Set-AzVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption FromImage
Modern alternative: Azure now supports managed disks, which simplify disk management. For managed disks, you can use:
1$vm = Set-AzVMOSDisk -VM $vm -Name "$vmName-osdisk" -StorageAccountType Standard_LRS -CreateOption FromImage
10. Create the Virtual Machine
Finally, create the VM with all configured settings.
1New-AzVM -ResourceGroupName $resourceGroup -Location $location -VM $vm
Note: VM creation typically takes 5-10 minutes to complete.
Connecting to Your New VM
After the VM is created, you can connect to it using Remote Desktop:
1Get-AzPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup | Select-Object IpAddress
Use the displayed IP address to connect via RDP.
Troubleshooting
- Deployment fails with quota errors: Check your subscription limits and request increases if needed
- Can’t connect to VM: Verify network security group rules allow RDP (port 3389) traffic
- Name already exists: Remember that storage account names must be globally unique
Wrap Up
You now have a fully functional Windows Server VM in Azure, created entirely through PowerShell. This approach makes it easy to replicate the deployment or incorporate it into CI/CD pipelines.
Next Steps
- Create a PowerShell script from these commands for easier reuse
- Explore adding data disks to your VM
- Set up monitoring and backup for your VM
- Learn how to use ARM templates for more complex deployments
You can find the full script example on GitHub here.
Have questions or suggestions for future Azure topics? Feel free to reach out!