- Courses
- đź’»Programming Languages
- 🪟PowerShell
- Enterprise-Grade PowerShell
- Infrastructure Automation
- Virtual Machines
On this page
PowerShell VM Automation: Azure, AWS & VMware Guide
Updated on June 27, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
PowerShell provides a unified automation framework for managing virtual machines across Azure, AWS, and VMware environments. For Azure, the Az.Compute module offers cmdlets like New-AzVM for streamlined provisioning, while AWS.Tools.EC2 provides New-EC2Instance for EC2 deployments. VMware environments are managed through PowerCLI with New-VM. Enterprise‑grade VM automation requires more than one‑off creation — it demands idempotent workflows that combine declarative infrastructure templates (Bicep, CloudFormation) with PowerShell orchestration, post‑deployment configuration via extensions or guest agents, and lifecycle management including start/stop scheduling and cost governance. Authentication should leverage managed identities (Azure) or IAM roles (AWS) to eliminate credential exposure. Mature automation also incorporates tagging standards, resource locks, and policy enforcement to maintain compliance at scale.
Azure Virtual Machine Automation​
The Az PowerShell module is Microsoft's recommended tool for managing Azure resources, including virtual machines. The Az.Compute submodule contains all VM‑related cmdlets.
Creating a Resource Group​
Every Azure VM must reside in a resource group, which serves as a logical container for deployment and management:
New-AzResourceGroup -Name "ProductionRG" -Location "japaneast"
The New-AzResourceGroup cmdlet creates the container with the specified name and region.
Provisioning a Virtual Machine​
The New-AzVM cmdlet is the primary tool for VM creation. It accepts parameters for the resource group, VM name, location, image, and network settings:
New-AzVm `
-ResourceGroupName "ProductionRG" `
-Name "WebServer01" `
-Location "japaneast" `
-Image "MicrosoftWindowsServer:WindowsServer:2022-datacenter-azure-edition:latest" `
-VirtualNetworkName "myVnet" `
-SubnetName "mySubnet" `
-SecurityGroupName "myNetworkSecurityGroup" `
-PublicIpAddressName "myPublicIpAddress" `
-OpenPorts 80,3389
When New-AzVM is invoked, it automatically creates dependent resources — virtual network, subnet, network security group, and public IP — if they do not already exist. During execution, you are prompted for the administrator username and password. The -OpenPorts parameter configures inbound rules for the specified ports — in this example, HTTP (80) for web traffic and RDP (3389) for remote management.
For scenarios requiring custom network configurations, you can pre‑create the network interface and pass it to the VM configuration:
$nic = New-AzNetworkInterface `
-Name "WebServer01-NIC" `
-ResourceGroupName "ProductionRG" `
-Location "japaneast" `
-SubnetId $subnet.Id
$vmConfig = New-AzVMConfig -VMName "WebServer01" -VMSize "Standard_B2s" |
Set-AzVMOperatingSystem -Windows -ComputerName "WebServer01" -Credential (Get-Credential) |
Set-AzVMSourceImage -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2022-datacenter" -Version "latest" |
Add-AzVMNetworkInterface -Id $nic.Id
New-AzVM -ResourceGroupName "ProductionRG" -Location "japaneast" -VM $vmConfig
This approach gives you full control over naming conventions for each resource component.
Understanding VM States and Cost Implications​
When automating VM lifecycle management, it is essential to understand the difference between stopping and deallocating a VM:
- Stopped (deallocated): The VM is shut down and compute resources are released. You are not charged for compute (only for storage and static IPs). This is the desired state for cost savings.
- Stopped (from inside the OS): The VM's operating system is shut down, but the compute allocation is still held. You continue to be charged for compute. This is not what you want.
Always use Stop-AzVM to deallocate VMs properly, rather than shutting down the operating system from within the VM.
Starting and Stopping VMs Programmatically​
To stop all running VMs in a resource group, use a script that filters by power state and optionally by tags for selective automation:
# Stop all VMs in a specific resource group
param(
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $false)]
[string]$TagName = "AutoShutdown",
[Parameter(Mandatory = $false)]
[string]$TagValue = "true"
)
# Authenticate using managed identity (for Azure Automation)
$null = Disable-AzContextAutosave -Scope Process
$AzureContext = (Connect-AzAccount -Identity).Context
Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
# Get all running VMs with the auto-shutdown tag
$vms = Get-AzVM -ResourceGroupName $ResourceGroupName -Status |
Where-Object { $_.PowerState -eq "VM running" -and $_.Tags[$TagName] -eq $TagValue }
foreach ($vm in $vms) {
Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $vm.Name -Force
}
The companion script to start VMs in the morning follows a similar pattern but uses Start-AzVM instead. Tag‑based filtering enables granular control — for example, shutting down only development VMs while leaving production VMs running.
Post‑Deployment Configuration​
Once a VM is provisioned, PowerShell can perform post‑deployment configuration through Azure VM Extensions:
Invoke-AzVMRunCommand `
-ResourceGroupName "ProductionRG" `
-VMName "WebServer01" `
-CommandId "RunPowerShellScript" `
-ScriptString "Install-WindowsFeature -Name Web-Server -IncludeManagementTools"
The Invoke-AzVMRunCommand cmdlet executes a PowerShell script on the target VM. This is useful for installing software, joining domains, or applying configuration settings. The -ScriptString parameter accepts the script to be executed, and the command runs within the guest operating system.
For more complex configurations, Azure Desired State Configuration (DSC) can be used to enforce and maintain consistent state across VM fleets.
Idempotent Deployment with Bicep​
While PowerShell can create VMs imperatively, enterprise automation typically combines declarative templates with PowerShell orchestration. Azure Bicep is a domain‑specific language for deploying Azure resources declaratively. Bicep deployments are idempotent — applying the same template multiple times results in the same resource state.
New-AzResourceGroupDeployment `
-ResourceGroupName "ProductionRG" `
-TemplateFile ".\vm.bicep" `
-Parameter @{ vmName = "WebServer01"; adminUsername = "azureuser" }
This approach ensures predictable, repeatable deployments across environments and prevents configuration drift.
AWS EC2 Automation​
AWS provides the AWS.Tools.EC2 module for managing EC2 instances. EC2 automation is more imperative than Azure — you call API operations directly.
Launching an EC2 Instance​
New-EC2Instance `
-ImageId "ami-0abcdef1234567890" `
-InstanceType "t3.micro" `
-MinCount 1 -MaxCount 1
The New-EC2Instance cmdlet launches EC2 instances with the specified AMI and instance type. The MinCount and MaxCount parameters control the number of instances launched.
Managing EC2 Lifecycle​
PowerShell can manage the full EC2 lifecycle — starting, stopping, resizing, attaching volumes, creating AMIs, and tagging resources:
# Stop an instance
Stop-EC2Instance -InstanceId $instanceId
# Start an instance
Start-EC2Instance -InstanceId $instanceId
# Tag for cost management and governance
New-EC2Tag -Resource $instanceId -Tag @{ Key = "Environment"; Value = "Production" }
IAM roles are the preferred authentication method for EC2 instances, providing temporary credentials without storing secrets.
VMware Virtual Machine Automation​
For on‑premises and private cloud environments, VMware PowerCLI provides the New-VM cmdlet for VM creation:
New-VM `
-Name "WebServer01" `
-VMHost "ESXi-01" `
-Datastore "Datastore01" `
-DiskGB 40 `
-MemoryGB 8 `
-NumCpu 2 `
-NetworkName "Virtual Machine Network"
The New-VM cmdlet supports specifying the VM name, host, datastore, CPU count, memory, disk size, and network. PowerCLI also provides cmdlets for managing VM power states (Start-VM, Stop-VM), snapshots, and cloning.
For Hyper‑V environments, the Hyper-V module provides New-VM, Start-VM, and Stop-VM cmdlets.
Designing Enterprise VM Automation Workflows​
Expert‑level VM automation is not about creating a single VM — it is about designing a repeatable, governed, and auditable process. Mature workflows include the following components.
Declarative Infrastructure​
Use Bicep for Azure, CloudFormation for AWS, or Terraform for multi‑cloud. PowerShell orchestrates deployments but does not replace declarative templates.
Configuration Management​
Apply post‑deployment configuration using Azure VM Extensions, AWS Systems Manager (SSM), PowerShell DSC, or custom bootstrap scripts. This ensures every VM is configured identically.
Identity and Access​
Leverage Azure managed identities, AWS IAM roles, or service principals. Never embed credentials in scripts. Retrieve secrets from SecretManagement vaults at runtime.
Governance and Compliance​
Implement tagging standards for cost allocation and automation. Apply resource locks to prevent accidental deletion. Enforce policies through Azure Policy or AWS Service Control Policies (SCPs). Log all operations for audit trails.
Idempotency​
Running the automation twice must not create duplicate VMs, break networking, or overwrite configuration unexpectedly. Idempotency is achieved through template‑based deployments, state validation, and conditional logic.
Summary​
Automating virtual machines with PowerShell requires a deep understanding of each platform's compute model and a commitment to enterprise‑grade patterns. Key principles include:
- Using Az.Compute for Azure, AWS.Tools.EC2 for AWS, and PowerCLI for VMware
- Authenticating with managed identities (Azure) or IAM roles (AWS) whenever possible
- Building VMs through structured configuration objects or declarative templates
- Applying post‑deployment configuration through extensions, guest agents, or DSC
- Designing workflows that are idempotent, auditable, and aligned with enterprise governance
- Automating start/stop schedules to optimise cloud costs
With these skills, PowerShell becomes a powerful, cloud‑agnostic automation engine capable of managing VM infrastructure at enterprise scale.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Cloud Resources
Next
Containers
-
Azure Virtual Machine Automation
- Creating a Resource Group
- Provisioning a Virtual Machine
- Understanding VM States and Cost Implications
- Starting and Stopping VMs Programmatically
- Post‑Deployment Configuration
- Idempotent Deployment with Bicep
-
AWS EC2 Automation
- Launching an EC2 Instance
- Managing EC2 Lifecycle
-
VMware Virtual Machine Automation
-
Designing Enterprise VM Automation Workflows
- Declarative Infrastructure
- Configuration Management
- Identity and Access
- Governance and Compliance
- Idempotency
-
Summary
