---
title: How to shut down your Virtual Machine if it is idle for 'X' hours on the basis of CPU Utilization Percentage
description: Read how you can create an automated shutdown of VM if it idle for 'X' hours below 'Y' average CPU percentage. Although the idle time is dependent on the requirement & company standards, it should ideally be around 1–2 hours of idle time.
image: https://blog.e-zest.com/hubfs/How%20To%20Shut%20Down%20Your%20Virtual%20Machine.jpg
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)
- [Innovation](https://blog.e-zest.com/innovation)
- [Healthcare](https://blog.e-zest.com/healthcare)
- [Media Kit](https://www.e-zest.com/media-kit)

# How to shut down your Virtual Machine if it is idle for 'X' hours on the basis of CPU Utilization Percentage

[Sourabh Majali](https://blog.e-zest.com/author/sourabh-majali)  Oct 05, 2020

[Microsoft Azure](https://blog.e-zest.com/topic/microsoft-azure) [Virtual Machine](https://blog.e-zest.com/topic/virtual-machine) [PowerShell Script](https://blog.e-zest.com/topic/powershell-script) [Azure Automation](https://blog.e-zest.com/topic/azure-automation)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization&text=How%20to%20shut%20down%20your%20Virtual%20Machine%20if%20it%20is%20idle%20for%20'X'%20hours%20on%20the%20basis%20of%20CPU%20Utilization%20Percentage>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization>

A Virtual Machine (VM) is an environment that replicates a physical machine, having its own allocation of processing unit, storage, and networking interface. VMs can co-exist with the physical machine it is installed on and allows remote access to data and interfaces. In the new normal, VMs are a great way to provide access to the central repository from virtual locations and help a business stay functioning.

However, like any other process, running VMs come with specific cost requirements, and idle VMs can add up to the company's overall budget. For heavy-duty projects or those requiring lots of remote collaboration and sharing of tools, we need virtual machines with massive CPUs and memory, which are quite expensive! Creating a workflow to shut down idle VMs a great step to ensure you get maximum benefit to managing cost and processing efficiently.

Read more to know how you can create an automated shutdown of VM if it idle for 'X' hours below 'Y' average CPU percentage. Although the idle time for a VM is dependent on the requirement and company standards, it should ideally be around 1 – 2 hours of idle time.

Let us explore a step-by-step method to achieve this:

**Prerequisites,**

-Virtual Machine  
-Azure Automation Account

The process is divided into 3 steps,

1. Create a Runbook with PowerShell script to Shut down a VM.
2. Create a Webhook.
3. Create an alert using Metric (CPU Percentage) less than X percent for Y time.

**Step 1. Create a Runbook using PowerShell Script**

Automation Account -\>Runbook -\>Create using PowerShell

Use the following PowerShell to create a Runbook to auto shutdown a VM,

#--------------------Start of Script -----------------------------------------------#

$connectionName = "AzureRunAsConnection"  
try  
{  
\# Get the connection "AzureRunAsConnection "  
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName   
"Logging in to Azure..."  
Add-AzureRmAccount \`  
 -ServicePrincipal \`  
 -TenantId $servicePrincipalConnection.TenantId \`  
 -ApplicationId $servicePrincipalConnection.ApplicationId \`  
 -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint   
}  
catch {  
if (!$servicePrincipalConnection)  
{  
$ErrorMessage = "Connection $connectionName not found."  
throw $ErrorMessage  
} else{  
Write-Error -Message $\_.Exception  
throw $\_.Exception  
} }  
\# to run this we need to confirm AzureRm.Profile and AzureRM.Insight is installed.  
$vm = Get-AzureRmVM -Name myVM -ResourceGroupName myResourceGroup1  
try{  
Stop-AzureRMvm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force  
Write-Host "Shutdown Successful"  
}  
catch  
{  
Write-Host "Error"  
}  
#------End of Script-------#

After adding the script, click 'Save' and 'Publish.'

![Virtual Machine Auto Shutdown](https://blog.e-zest.com/hubfs/image-png-2.png)

**Step 2. Create a Webhook**

Automation Account -\> Select your Runbook -\> Webhook -\>Add,

Webhooks allow us to extract more from Azure Alerts. We can specify an HTTP or HTTPS endpoints as a webhook while creating or updating an alert on the Azure Portal. Once the URL is hit, the respective Runbook is triggered, henceforth running the PowerShell script we created in Step1.

Add Name and save the URL of the Webhook in a safe place that we will use in Step 3.

![VM- Create new webhook](https://blog.e-zest.com/hubfs/image-png-3.png)

Make sure you have copied the URL of Webhook.

**Step 3. Create an Alert with conditions**

All Resources -\> “YourVMname” -\> Alerts -\> New Alert Rule,

Creating an Alert Rule proactively notifies you of desired conditional data in your collected monitoring data. We can create our own action group & trigger alerts based on data that is collected. Further, it is divided into 3 parts as follows

1. Condition
2. Action Group
3. Alert Rule Details

**a. Condition (Select Condition)**

While creating an Alert, we need to provide condition based on Metric Signal type & "Percentage CPU" Signal name, refer the following image,

![VM - Configure signal logic](https://blog.e-zest.com/hubfs/image-png-4.png)

Once we select the above signal, a new window is introduced where we add logic to our alert,

![VM - Alert Logic](https://blog.e-zest.com/hubfs/image-png-Oct-06-2020-06-39-19-88-AM.png)

The above example shows the operator as 'Less Than,' aggregation type as 'Average' & Threshold, e.g., is 10 here our case. Further, Granularity (Period) is set to 1 hour, and 'Frequency' is 15 Minutes.

The alert will be triggered according to the above setting only if the average CPU percentage is less than 10 % for 1 hours duration is matched.

**b. Action Group -\> Create Action Group**

Action Groups within Azure Alerts are typically a group of notifications preferences and actions to be taken once the condition is matched of alerts and alert is triggered, which are used by both Azure Monitor and service alerts. Select the notification type to "E-mail/SMS/Push/Voice" & add E-mail address as Notification type as below snap,

![VM - Create action group](https://blog.e-zest.com/hubfs/image-png-Oct-06-2020-06-39-56-54-AM.png)

Once we select Actions, type "Webhook" & Paste here Webhook URL we created in Step2. This will help us to trigger the Webhook URL and eventually run the PowerShell script.

![VM - create action group](https://blog.e-zest.com/hubfs/image-png-Oct-06-2020-06-40-47-04-AM.png)

**c. Alert Rule Details -\> Add details & click Create**

Adding relevant details to the Alert rule name and description will help us out in identifying specific alerts. We can keep severity from 0 to 5, where 0 being high and 5 being low.

Once the details are filled, we can click on "Create Alert Rule" this will created and enable the rule which will start monitoring our mentioned condition.

![VM - alert rule details](https://blog.e-zest.com/hubfs/image-png-Oct-06-2020-06-41-38-32-AM.png)

Output: An e-mail is sent to the mentioned e-mail address in the action group in Step3b.

![VM - alert](https://blog.e-zest.com/hubfs/image-png-Oct-06-2020-06-42-07-50-AM.png)

Following the above steps helps us create an alert that is triggered if the CPU percentage is less than X for Y time duration. The triggered alert will generate an e-mail & trigger a webhook URL, which will run the PowerShell script we created to shut down the VM.

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization&text=How%20to%20shut%20down%20your%20Virtual%20Machine%20if%20it%20is%20idle%20for%20'X'%20hours%20on%20the%20basis%20of%20CPU%20Utilization%20Percentage>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/auto-stop-your-vm-based-on-cpu-utilization&title=&summary=&source=>

### Similar Blog

[![Enabling Azure Storage Management Lifecycle for Efficiency and Cost Optimization](https://blog.e-zest.com/hubfs/Banners/Enabling%20Azure%20Storage%20Management%20Lifecycle%20for%20Efficiency%20and%20Cost%20Optimization.jpg) Enabling Azure Storage Management Lifecycle for Efficiency and Cost Optimization Efficient data management is vital for all organizations, particularly as data continues to proliferate. Azure Storage Management ... By Sourabh Majali Sep 29, 2023](https://blog.e-zest.com/enabling-azure-storage-management-lifecycle-for-efficiency-and-cost-optimization)

[![How to Enable Accelerated Networking on NICs – Azure Virtual Machine using PowerShell + Excel sheet](https://blog.e-zest.com/hubfs/Blog_Files/blog_image05_2x_2560x1334.jpg) How to Enable Accelerated Networking on NICs – Azure Virtual Machine using PowerShell + Excel sheet Accelerated Networking enables single root I/O virtualization (SR-IOV) to a VM, greatly improving its networking performance. ... By Sourabh Majali Jul 07, 2023](https://blog.e-zest.com/how-to-enable-accelerated-networking-on-nics-azure-virtual-machine-using-powershell-excel-sheet)

[![Building Cloud First Apps Using Microsoft Azure](https://blog.e-zest.com/hubfs/blog-files/Building%20cloud%20first%20blog.png) Building Cloud First Apps Using Microsoft Azure The COVID-19 pandemic required most of the organizations to adopt a remote working environment. This made people realize the ... By Corporate Jul 06, 2020](https://blog.e-zest.com/building-cloud-first-apps-using-microsoft-azure)

[![Virtual Machine Scale Set in Azure Microsoft](https://blog.e-zest.com/hubfs/blog-files/virtual-machine-scale-set-in-azure-microsoft.jpg) Virtual Machine Scale Set in Azure Microsoft Virtual Machine Scale Set, an interesting service offered by Microsoft Azure, helps to create and manage a group of identical, ... By Surbhi Agarwal Jan 16, 2020](https://blog.e-zest.com/virtual-machine-scale-set-in-azure-microsoft)

[![Enabling Azure Storage Management Lifecycle for Efficiency and Cost Optimization](https://blog.e-zest.com/hubfs/Banners/Enabling%20Azure%20Storage%20Management%20Lifecycle%20for%20Efficiency%20and%20Cost%20Optimization.jpg) Enabling Azure Storage Management Lifecycle for Efficiency and Cost Optimization Efficient data management is vital for all organizations, particularly as data continues to proliferate. Azure Storage Management ... By Sourabh Majali Sep 29, 2023](https://blog.e-zest.com/enabling-azure-storage-management-lifecycle-for-efficiency-and-cost-optimization)

[![PowerShell – Assigning Tags to Azure Resources using Excel Sheet](https://blog.e-zest.com/hubfs/Banners/PowerShell%20%E2%80%93%20Assign%20Tags%20to%20Azure%20Resources%20using%20Excel%20Sheet%20V2-08_2x.jpg) PowerShell – Assigning Tags to Azure Resources using Excel Sheet Prerequisite By Sourabh Majali Aug 03, 2023](https://blog.e-zest.com/powershell-assigning-tags-to-azure-resources-using-excel-sheet)

[![How to Enable Accelerated Networking on NICs – Azure Virtual Machine using PowerShell + Excel sheet](https://blog.e-zest.com/hubfs/Blog_Files/blog_image05_2x_2560x1334.jpg) How to Enable Accelerated Networking on NICs – Azure Virtual Machine using PowerShell + Excel sheet Accelerated Networking enables single root I/O virtualization (SR-IOV) to a VM, greatly improving its networking performance. ... By Sourabh Majali Jul 07, 2023](https://blog.e-zest.com/how-to-enable-accelerated-networking-on-nics-azure-virtual-machine-using-powershell-excel-sheet)

[![Integration of your Web App with Virtual Network](https://blog.e-zest.com/hubfs/blog-files/Integration%20of%20Web%20App%20with%20Virtual%20Network-190822%20(1).png) Integration of your Web App with Virtual Network This blog describes how to connect **Web App** with **Virtual Network**. This integration allows your WebApp to get access to all ... By Sourabh Majali Aug 29, 2019](https://blog.e-zest.com/integration-of-your-web-app-with-virtual-network)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.