Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

Find the Stopped Azure VMs and Deallocating Them

May 19, 2024
in Blog, Microsoft Azure
0
ADVERTISEMENT

Table of Contents

Even when stopped, Azure VMs that are not deallocated continue to incur costs. This article explores methods to find the stopped Azure VMs and deallocating them.

Find Stopped Azure VMs with PowerShell

When an Azure VM is shutdown using the operating system it enters a “stopped” state. In this state, the server is not running but the compute resource is still being held by Azure so you continue to get charged for that compute. Virtual Machines should be deallocated by either stopping them in the Portal or using a PowerShell Stop-AzVM cmdlet.

XBIGCengJOmkd8TpLux2lOYD4QXcfZ5UDhZZaL4jPntVbGlf5uuwhaI0ZMSV

PowerShell can be used to look up the power state of VMs in your Azure Subscription as follows:

$array = @()
foreach ($vm in Get-AzVM) {
        $object = New-Object -TypeName PSObject -Property $([ordered]@{
            VMName          = $vm.Name
            PowerState      = (Get-AzVM -VMName $($vm.Name) -Status).PowerState
            ResourceGroup   = $vm.ResourceGroupName
        })
        $array += $object
    }
$array

The results might look like below.:

  • The virtual machine vm-1162587192,power state is ‘VM deallocated‘. We’ll not be charged for this VM because it’s deallocated.
  • The virtual machine vm-1555300244 power state is ‘VM Stopped’. It’s not deallocated, so we continue to get charged for that compute even it has been stopped.
VMName        PowerState     ResourceGroup
------        ----------     -------------
vm-1162587192 VM deallocated RG-1162587192
vm-1185303510 VM running     RG-1185303510
vm-1555300244 VM stopped     RG-1555300244
vm-210251813  VM running     RG-210251813

We want to find the stopped VM (without deallocated). So, we use the Where-Object cmdlet to filter for those that are stopped but not deallocated.

$array = @()
foreach ($vm in Get-AzVM) {
        $object = New-Object -TypeName PSObject -Property $([ordered]@{
            VMName          = $vm.Name
            PowerState      = (Get-AzVM -VMName $($vm.Name) -Status).PowerState
            ResourceGroup   = $vm.ResourceGroupName
        })
        if (($object.PowerState -eq "VM stopped")) {
          $array += $object
        }
    }
$array
VMName        PowerState ResourceGroup
------        ---------- -------------
vm-1555300244 VM stopped RG-1555300244

Finally, we can proceed to properly shut down those VMs and deallocate their resources. The below script will find all stopped VMs then deallocating them.

$array = @()
foreach ($vm in Get-AzVM) {
        $object = New-Object -TypeName PSObject -Property $([ordered]@{
            VMName          = $vm.Name
            PowerState      = (Get-AzVM -VMName $($vm.Name) -Status).PowerState
            ResourceGroup   = $vm.ResourceGroupName
        })
        if (($object.PowerState -eq "VM stopped")) {
          $array += $object
        }
    }
$array

foreach ($stopedVM in $array) {
    Write-Host "Stopping VM $($stopedVM.VMName)" -ForegroundColor Yellow
    Stop-AzVM -Name $($stopedVM.VMName) -ResourceGroupName $($stopedVM.ResourceGroup) -Force
}

As you can see, the power state of the VM was changed from ‘VM stopped‘ to ‘VM deallocated‘. It means you will not be charged for the VM anymore.

VMName        PowerState     ResourceGroup
------        ----------     -------------
vm-1162587192 VM deallocated RG-1162587192
vm-1185303510 VM running     RG-1185303510
vm-1555300244 VM deallocated RG-1555300244
vm-210251813  VM running     RG-210251813
0OknTu9Uo8yVAykDJ3GhGrV0Ml8tb2hjXwSsfC6bsGKqlJED4eEh66IDli6C
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

Rate this post
Previous Post

How to Find Stopped Azure VMs with PowerShell

Next Post

What’s the Difference Between Deallocated and Stopped of Azure VMs

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory