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:
|
|
Step-by-Step VM Creation
1. Create a Resource Group
Resource groups serve as logical containers for Azure resources, making management and organization easier.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
Tip: Use
Get-AzVMSize -Location $location
to see available VM sizes in your region.
6. Set Virtual Machine Credentials
Create secure login credentials for your VM.
|
|
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.
|
|
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.
|
|
9. Create Disk for Virtual Machine
Configure the OS disk for your VM.
|
|
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.
|
|
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:
|
|
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!