Automation Runbook To Shutdown Azure Resource Manager (ARM) VM

Here I will be sharing an automation runbook which you can parameterise to shut down VMs within a subscription based on a resource group using tags.   This article will not be a how to guide but a reference point for this script. 

You may add a parameter for the resource groups or you could even use a loops to run through all the resource groups by modifying this code.

If for example you have tags on your VMs such as Autoshutdown : YES then the runbook will pick these VMs to shut down.

Automation Runbook to Stop Azure VMs by Tag



        Stops all the Azure VMs in a specific Azure Resource Group if autoshutdown tag is set to Yes



        This sample runbooks stops all of the virtual machines in the specified Azure Resource Group.

        For more information about how this runbook authenticates to your Azure subscription, see the

        Microsoft documentation here:


    .PARAMETER ResourceGroupName

        Name of the Azure Resource Group containing the VMs to be stopped.

        Shutdown value – Can be set to YES by default or use a time zone for example CET and pass this in as a param and run at required time.  Useful when the group of Vms have different time zone requirements.



        This runbook will only return VMs deployed using the new Azure IaaS features available in the Azure Preview Portal and Azure Resource Manager templates. For more information, see



        AUTHOR: Mitesh Chauhan 02/08/2016

        LASTEDIT: 17/1/2016



workflow shutdown-dev-vms




        [String] $ResourceGroupName  = “RG NAME”,


       #The name of the Automation Credential Asset this runbook will use to authenticate to Azure.


        [String] $CredentialAssetName  = “Azure Automation Cred”,



        [String] $SubscriptionID  = “Sub ID”,



        [String] $Shutdownvalue  = “Yes”



    #Get the credential with the above name from the Automation Asset store

    $Cred = Get-AutomationPSCredential -Name $CredentialAssetName

    if(!$Cred) {

        Throw “Could not find an Automation Credential Asset named ‘${CredentialAssetName}‘. Make sure you have created one in this Automation Account.”



Login-AzureRmAccount -Credential $Cred

Select-AzureRmSubscription -Subscriptionid $SubscriptionID


# To get all VMs in a subscription use the following line.

# Also change stop line to Stop-AzureRmVM -ResourceGroupName $vm.ResourceGroupName

# $vmList = Find-AzureRmResource -TagName Autoshutdown -TagValue $Shutdownvalue | Where-Object {$_.ResourceType -eq “Microsoft.Compute/virtualMachines”} | Select Name, ResourceGroupName


# Gather all VMs with auto shutdown tag set to the tag value in the parameters

$vmList = Find-AzureRmResource -TagName Autoshutdown -TagValue $Shutdownvalue | Where-Object {$_.ResourceGroupName -eq $ResourceGroupName -and $_.ResourceType -eq “Microsoft.Compute/virtualMachines”} | Select Name, ResourceGroupName


foreach ($VM in $vmlist)


                $PowerState = (Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VM.Name -Status -ErrorAction $ErrorActionPreference -WarningAction $WarningPreference).Statuses.Code[1]


                if ($PowerState -eq ‘PowerState/deallocated’)


                    $VM.Name + ” is already shut down.”




                    $VM.Name + ” is being shut down.”

                    $ShutdownState = (Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VM.Name -Force -ErrorAction $ErrorActionPreference -WarningAction $WarningPreference).IsSuccessStatusCode

                    start-sleep -Seconds 10                             

                              if ($ShutdownState -eq ‘True’)


                    $VM.Name + ” Has been shut down successfully.”




                    $VM.Name + ” Has failed to shut down. Shutdown Status  = “ + $ShutdownState




Write-output “Script complete”



Thank You For Visiting.