Azure Virtual Machines PowerShell Reference
Use Azure PowerShell to deploy and manage Azure Virtual Machines (VMs). This documentation provides comprehensive cmdlets for creating, configuring, and managing your virtual machines and their associated resources.
Getting Started with Azure VMs and PowerShell
Azure PowerShell provides a robust set of cmdlets to interact with Azure resources. To manage VMs, you'll typically use cmdlets starting with Get-AzVM
, New-AzVM
, Update-AzVM
, and Remove-AzVM
.
Tip: Ensure you have the latest Azure PowerShell module installed. You can update it using
Update-Module -Name Az
.
Common PowerShell Tasks
1. Connecting to Azure
Before you can manage resources, you need to connect to your Azure account.
# Install the Az module if you haven't already
# Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
# Connect to your Azure account
Connect-AzAccount
# Select the subscription you want to use (if you have multiple)
# Set-AzContext -SubscriptionId "YOUR_SUBSCRIPTION_ID"
2. Creating a Virtual Machine
Create a new virtual machine with specific configurations.
# Define VM configuration
$vmConfig = New-AzVMConfig -VMName "MyVM" -VMSize "Standard_DS1_v2" `
-ImageName "Win2019Datacenter" -Location "East US" `
-Credential (Get-Credential)
# Create the VM
New-AzVM -VM $vmConfig -ResourceGroupName "MyResourceGroup"
3. Managing VM Disks
Manage operating system and data disks attached to your VMs.
# Get a VM
$vm = Get-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM"
# Add a new data disk
$diskConfig = New-AzVMDataDisk -Name "MyDataDisk" -CreateOption Empty -DiskSizeGB 100 -Lun 0
Add-AzVMDataDisk -VM $vm -DataDisk $diskConfig
Update-AzVM -VM $vm -ResourceGroupName "MyResourceGroup"
4. Managing VM Networking
Configure network interfaces, public IPs, and load balancers.
# Get the network interface for a VM
$nic = Get-AzNetworkInterface -ResourceGroupName "MyResourceGroup" -Name "MyVMVMNic"
# Assign a public IP address
$publicIp = New-AzPublicIpAddress -Name "MyVMPublicIP" -ResourceGroupName "MyResourceGroup" -Location "East US" -AllocationMethod Dynamic
$nic.IpConfigurations[0].PublicIpAddress = $publicIp
Set-AzNetworkInterface -NetworkInterface $nic
Note: For creating and managing resources like network interfaces and public IPs, you'll use cmdlets from the
Az.Network
module.
Key PowerShell Cmdlets for Azure VMs
New-AzVM
: Creates a new virtual machine.Get-AzVM
: Retrieves information about virtual machines.Update-AzVM
: Updates an existing virtual machine.Remove-AzVM
: Deletes a virtual machine.New-AzVMConfig
: Creates a VM configuration object.Add-AzVMDataDisk
: Adds a data disk to a VM configuration.Set-AzVMSourceImage
: Sets the OS image for a VM.Get-AzVMImageSku
: Retrieves SKU information for VM images.Get-AzVMSize
: Retrieves available VM sizes.