PowerShell common commands for Azure VMs

To get a list of resource groups in your subscription, use Get-AzureRmResourceGroup.

—–

List VMs in a subscriptionGet-AzureRmVM

Filter List:

$armVms = Get-AzurermVM | select Name, ServiceName, ResourceGroupName
$armVms

—–

List VMs in a resource group, Get-AzureRmVM -ResourceGroupName myResourceGroup

—-

Get information about a VM, Get-AzureRmVM -ResourceGroupName myRG -Name $myVM

—-

To get the virtual machine core count usage for a location: Get-AzureRmVMUsage

Manage VMs

Task Command
Start a VM Start-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Stop a VM Stop-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Restart a running VM Restart-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Delete a VM Remove-AzureRmVM -ResourceGroupName $myResourceGroup -Name $myVM
Generalize a VM Set-AzureRmVm -ResourceGroupName $myResourceGroup -Name $myVM -Generalized

Run this command before you run Save-AzureRmVMImage.

Capture a VM Save-AzureRmVMImage -ResourceGroupName $myResourceGroup -VMName $myVM -DestinationContainerName “myImageContainer” -VHDNamePrefix “myImagePrefix” -Path “C:\filepath\filename.json”

A virtual machine must be prepared, shut down and generalized to be used to create an image. Before you run this command, run Set-AzureRmVm.

Update a VM Update-AzureRmVM -ResourceGroupName $myResourceGroup -VM $vm

Get the current VM configuration using Get-AzureRmVM, change configuration settings on the VM object, and then run this command.

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/ps-common-ref

Leave a comment