Manage Learn to apply best practices and optimize your operations.

Azure PowerShell cmdlets monitor, manage VMs

PowerShell gives administrators a way to customize reports that hone in on the details that matter to the business, such as the configuration of production VMs.

When multiple workloads lift off into the cloud, administrators can maintain control over these critical virtual...

machines by sidestepping the admin portal.

Azure PowerShell cmdlets help speed up the execution of everyday tasks, such as bulk edits to VMs or report generation without resorting to GUI management. PowerShell not only reduces the time it takes to run tasks in the Azure portal, but it automates regular tasks.

For example, to reduce costs, you can create a PowerShell script that collects the status of each Azure VM every day to ensure only the required VMs have permission to run. The following samples offer a starting point that administrators can build from to develop scripts that are more comprehensive.

How to install and use Azure PowerShell

Managing Azure VMs in Windows PowerShell requires installing the Azure Resource Manager PowerShell module on a Windows computer with the Microsoft Web Platform Installer or the PowerShell Gallery. Using the PowerShell Gallery requires the PowerShellGet module, which is part of Windows 10 and Windows Server 2016.

To install the Azure PowerShell module with PowerShell Gallery, run the following PowerShell commands:

Install-Module AzureRM
Install-Module Azure
Import-Module Azure

Next, log in to your Azure account with the Login-AzureRMAccount cmdlet, which prompts for your credentials. To check the login status, run the Get-AzureRMSubscription cmdlet, which shows the details of the logged in Azure subscription.

Azure PowerShell cmdlets manage cloud VMs

Azure PowerShell cmdlets enable administrators to run several actions against Azure VMs, such as moving a VM, resizing a VM and modifying a VM's settings.

To get a list of PowerShell cmdlets specifically for Azure VM, execute this command:

Get-Command -Module AzureRM *-AzureRMVM*

Find all Azure VMs in a subscription

To list all the VMs in your Azure subscription, run the Get-AzureRMVM cmdlet.

As the name suggests, the cmdlet displays all the running and non-running VMs from Azure in the current PowerShell window.

To export the results to a CSV file, use the following command:

Get-AzureRMVM | Export-CSV C:\Temp\AllAzureVMInfo.CSV -NoType

Collect Azure VM information

To gather details about a particular Azure VM in a resource group, use the following command with the name of the resource group and VM:

Get-AzureRMVM -ResourceGroupName "AzureResourceGroup" -Name "VM1"

To collect information about all the Azure VMs in a resource group, use this command:

PowerShell basics: Objects

Objects provide the building blocks for PowerShell scripts.

Get-AzureRMVM -ResourceGroupName "AzureResourceGroup"

To export the results to a CSV file, just add the Export-CSV cmdlet as shown in the command below:

Get-AzureRMVM -ResourceGroupName "AzureResourceGroup" | Export-CSV C:\Temp\AzureVMInfo.CSV -NoType

Stop and start Azure VMs

The Start-AzureRMVM and Stop-AzureRMVM Azure PowerShell cmdlets start and stop VMs. To handle multiple VMs, a simple PowerShell ForEach loop processes the VMs specified in a text file.

In the example below, PowerShell uses a text file with the list of VMs to process. This script and the other examples in this article target a specific Azure resource group named AzureRSGroup. Modify the name of your resource group when using it in your Azure subscription.

$VMFile = "C:\Temp\AzureVMs.txt"
$AzureResourceGroup = "AzureRSGroup"

This section of the PowerShell script stops several Azure VMs listed in the text file:

Foreach ($ThisVM in Get-Content "$VMFile")
Stop-AzureRMVM -ResourceGroupName $AzureResourceGroup -Name "$ThisVM"

To start the Azure VMs in the text file, execute this PowerShell script:

Foreach ($ThisVM in Get-Content "$VMFile")


Start-AzureRMVM -ResourceGroupName $AzureResourceGroup -Name "$ThisVM"


Get the Azure VM status

There are times you need to determine the current state of an Azure VM to check if it is running or deallocated. The following PowerShell script gathers the provisioning state of Azure VMs listed in a text file:

$VMFile = "C:\Temp\AllVMs.txt"
$ReportFile = "C:\Temp\VMStatus.CSV"
$STR = "VM Name, Status"
Add-Content $ReportFile $STR
$AzureResourceGroup = "AzureRSGroup"
Foreach ($ThisVM in Get-Content "$VMFile")
$VMStatusCode = $(Get-AzureRmVM -ResourceGroupName $AzureResourceGroup -VMName $ThisVM -Status).Statuses
foreach($EachStatusNow in $VMStatusCode)
if($EachStatusNow.Code -ne "ProvisioningState/succeeded")
$FinalStatus = "status is `"$($EachStatusNow.displaystatus)`""
$STR = $ThisVM+","+$FinalStatus
Add-Content $ReportFile $STR

The script generates a report in a C:\Temp\VMStatus.CSV file with the Azure VM name and its status.

Find the uptime for an Azure VM

Azure PowerShell doesn't offer a cmdlet to get uptime for Azure VMs, but you can determine it with the Win32_ClassOperatingSystem Windows Management Instrumentation class in the following PowerShell script:

$VMFile = "C:\Temp\AllVMIPs.txt"
$ReportFile = "C:\Temp\AzureVMUpTime.CSV"
$STR = "VM IP, Up Time"
Add-Content $ReportFile $STR
Foreach ($ThisVM in Get-Content "$VMFile")
$NewCIMSession = New-CimSession -ComputerName $ThisVM
$VMOS = Get-CimInstance -CimSession $NewCIMSession -ClassName Win32_OperatingSystem
$uptime = (Get-Date) - $VMOS.LastBootUpTime
$STR = $ThisVM+","+$uptime
Add-Content $ReportFile $STR

This PowerShell script collects uptime for Azure VMs with public endpoints. The text file contains the list of public IPs for the Azure VMs. The script puts uptime results in a CSV file.

Prepare an Azure VM configuration

It's helpful to get a report to verify that production Azure VM configurations match the required settings. At a minimum, you want specific Azure VM information, such as the hardware profile, location, size, operating system type, virtual network card, boot diagnostics status and provisioning state.

The following PowerShell script queries VMs listed in a text file and produces a CSV report with the properties for each VM:

$VMFile = "C:\Temp\AllVMs.txt"
$ReportFile = "C:\Temp\AzureVMConfigReport.CSV"
$STR = "VM Name, Resource Group Name, Hardware Profile, VM Location, Boot Diagnostics Status, OS Type, vNIC, Provisioning State"
Add-Content $ReportFile $STR
$AzureResourceGroup = "AzureRSGroup"
Foreach ($ThisVM in Get-Content "$VMFile")
$VMProp = Get-AzureRMVM -Name $ThisVM -ResourceGroupName $AzureResourceGroup
$VMLocation = $VMProp.Location
$BootDStatus = $VMProp.DiagonosticsProfile.BootDiagnostics.Enabled
$VMProvState = $VMProp.ProvisioningState
$VMHWProfile = $VMProp.HardwareProfile.VMSize
$VMOSType = $VMProp.OSType
$STR = $ThisVM+","+$AzureResourceGroup+","+$VMHWProfile+",

Add-Content $ReportFile $STR

This was last published in April 2018

Dig Deeper on Microsoft cloud and virtualization

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

What handy PowerShell snippets do you use to manage or monitor VMs in Azure?