Azure PowerShell Examples: Virtual Machines
Explore practical examples of using Azure PowerShell cmdlets to manage your virtual machines. From creation and configuration to scaling and monitoring, these scripts provide a solid foundation for automating your Azure VM operations.
Create and Configure a New Virtual Machine
This example demonstrates how to create a basic Windows VM in Azure, including specifying resource group, location, VM size, and credentials.
# Define variables
$resourceGroupName = "MyResourceGroup"
$location = "East US"
$vmName = "MyWinVM"
$vmSize = "Standard_DS1_v2"
$adminUsername = "azureuser"
$adminPassword = ConvertTo-SecureString "YourSecurePassword123!" -AsPlainText -Force
# Create a resource group if it doesn't exist
New-AzResourceGroup -Name $resourceGroupName -Location $location -Force
# Create a virtual machine
New-AzVM `
-ResourceGroupName $resourceGroupName `
-Name $vmName `
-Location $location `
-ImageWin2019Datacenter `
-Size $vmSize `
-Credential (New-Object System.Management.Automation.PSCredential($adminUsername, $adminPassword)) `
-OpenPorts 3389 # RDP
Write-Host "VM '$vmName' created successfully in resource group '$resourceGroupName'."
Start, Stop, and Restart a Virtual Machine
Manage the lifecycle of your existing Azure VMs. This script shows how to start a deallocated VM, stop a running VM, and restart a VM.
# Define variables
$resourceGroupName = "MyResourceGroup"
$vmName = "MyWinVM"
# Get the VM object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
# Start the VM
# Start-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
# Stop the VM (deallocate it to save costs)
# Stop-AzVM -ResourceGroupName $resourceGroupName -Name $vmName -Force
# Restart the VM
Restart-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
Write-Host "VM '$vmName' has been restarted."
Attach a New Data Disk to a Virtual Machine
Expand the storage capacity of your VM by attaching a new managed data disk. This script creates a new managed disk and attaches it to an existing VM.
# Define variables
$resourceGroupName = "MyResourceGroup"
$vmName = "MyWinVM"
$diskName = "MyDataDisk"
$diskSizeGB = 100
$lun = 0 # Logical Unit Number for the disk
# Get the VM object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
# Create a new managed disk
$disk = New-AzDisk `
-DiskName $diskName `
-ResourceGroupName $resourceGroupName `
-Location $vm.Location `
-CreateOption Empty `
-DiskSizeGB $diskSizeGB `
-StorageAccountType Standard_LRS
# Create the disk configuration
$diskConfig = New-AzVMDataDisk `
-Name $diskName `
-CreateOption Attach `
-ManagedDiskId $disk.Id `
-Lun $lun
# Update the VM with the new data disk
Add-AzVMDataDisk `
-VM $vm `
-DataDisk $diskConfig
# Update the VM in Azure
Update-AzVM `
-ResourceGroupName $resourceGroupName `
-VM $vm
Write-Host "Data disk '$diskName' attached to VM '$vmName'."
Get Public IP Address of a Virtual Machine
Retrieve the public IP address assigned to your Azure VM, which is essential for connecting to it from the internet.
# Define variables
$resourceGroupName = "MyResourceGroup"
$vmName = "MyWinVM"
# Get the VM object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
# Get the network interface of the VM
$nic = Get-AzNetworkInterface -ResourceGroupName $resourceGroupName -Name $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/')[-1]
# Get the public IP configuration from the NIC
$ipConfig = $nic.IpConfigurations[0]
# Get the public IP address object
$publicIp = Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1]
Write-Host "Public IP Address for '$vmName': $($publicIp.IpAddress)"