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

How to Check Azure VM Power State Using PowerShell

May 24, 2024
in Blog, Microsoft Azure
0
ADVERTISEMENT

Table of Contents

Check Azure VM Power State Using 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. This article looks at how to check Azure VM power state using PowerShell.

XBIGCengJOmkd8TpLux2lOYD4QXcfZ5UDhZZaL4jPntVbGlf5uuwhaI0ZMSV

There are the below Power State for the Azure VM:

  • Starting − The virtual machine is being started.
  • Running − The virtual machine is currently running
  • Stopping − The virtual machine is being stopped
  • Stopped − The virtual machine is currently stopped and but still incur compute charges.
  • Deallocating − The virtual machine is being deallocated.
  • Deallocated − The virtual machine is deallocated and released all the resources and does not incur the charges.

To check if the VMs are running, deallocated, or stopped using PowerShell, we need to use the – Status parameter. If you write only the Get-AzVM command to get the VM details, it won’t show up the Azure VM power status default.

For the specific VM power state, you need to provide a particular VM name:

Get-AzVM -VMName 'vm-1381113815' -Status | FL
ResourceGroupName Name          Location PowerState
----------------- ----          -------- ----------
RG-1381113815     vm-1381113815 eastus   VM stopped

To check the power state of all your Azure VMs, you can use the following code snippet. It will get the properties of all VMs in a subscription then use the foreach loop to get the power state of each VM.

$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
            Location        = $vm.Location
        })
        $array += $object
    }

$array | Format-Table

The results might look like this:

VMName        PowerState     ResourceGroup Location
------        ----------     ------------- --------
vm-1381113815 VM stopped     RG-1381113815 eastus
vm-7342448    VM running     RG-7342448    eastus
vm-1494803974 VM deallocated RG-1494803974 germanywestcentral
vm-1892006009 VM running     RG-1892006009 germanywestcentral
vm-660759503  VM deallocated RG-660759503  germanywestcentral

Use the Statuses property in Get-AzVM cmdlet

Alternatively, we can use the Statuses property in Get-AzVM cmdlet to get the power states.

Get-AzVM -Name 'vm-1381113815' -ResourceGroupName 'RG-1381113815' -Status

When using this method, we can get more information about the virtual machine such as HyperVGeneration, Disks, Extensions and VMAgent.

ResourceGroupName          : RG-1381113815
Name                       : vm-1381113815
HyperVGeneration           : V2
Disks[0]                   : 
  Name                     : vm-1381113815_OsDisk_1_21997f46f7a54a72a00082cd6ccff543
  Statuses[0]              :
    Code                   : ProvisioningState/succeeded
    Level                  : Info
    DisplayStatus          : Provisioning succeeded
    Time                   : 5/18/2024 2:39:18 PM
Extensions[0]              :
  Name                     : BGInfo
VMAgent                    :
  VmAgentVersion           : Unknown
  Statuses[0]              :
    Code                   : ProvisioningState/Unavailable
    Level                  : Warning
    DisplayStatus          : Not Ready
    Message                : VM Agent is unresponsive.
    Time                   : 5/18/2024 4:05:29 PM
Statuses[0]                :
  Code                     : ProvisioningState/succeeded
  Level                    : Info
  DisplayStatus            : Provisioning succeeded
  Time                     : 5/18/2024 2:48:03 PM
Statuses[1]                :
  Code                     : PowerState/stopped
  Level                    : Info
  DisplayStatus            : VM stopped

Below is a sample PowerShell script that gets the power state of all VMs using the Statuses property:

$array = @()
foreach ($vm in Get-AzVM) {
    $vmName         = $vm.Name
    $resourceGroup  = $vm.ResourceGroupName
    $vmNicName      = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[8]
    $ipAddress      = Get-AzPublicIpAddress | Where-Object {$_.IpConfiguration.Id -like "*$vmNicName*"}
    $powerState     = Get-AzVM -Name $vmName -ResourceGroupName $resourceGroup -Status | Select @{n="Status"; e={$_.Statuses[1].DisplayStatus}}

    $object = New-Object -TypeName PSObject -Property $([ordered]@{
        VMName          = $vm.Name
        PowerState      = $powerState.Status
        IpAddress       = $ipAddress.IpAddress
        ResourceGroup   = $resourceGroup
        Location        = $vm.Location
    })
    $array += $object
}

$array | Format-Table
# The sample output
VMName        PowerState     IpAddress     ResourceGroup Location
------        ----------     ---------     ------------- --------
vm-1381113815 VM stopped     13.90.206.15  RG-1381113815 eastus
vm-7342448    VM running     13.90.206.132 RG-7342448    eastus
vm-1494803974 VM deallocated 4.185.144.22  RG-1494803974 germanywestcentral
vm-1892006009 VM running     4.185.41.217  RG-1892006009 germanywestcentral
vm-660759503  VM deallocated 4.185.42.113  RG-660759503  germanywestcentral
ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

How to Get the Azure VM’s Public IP Address Using PowerShell

Next Post

How to Find Stopped Azure VMs with PowerShell

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