Categories Articles

PowerShell Quick Tip: Validate the status of a virtual machine

More often than you might expect, you may have to validate if any given virtual machine is running or stopped (deallocated) in Microsoft Azure. I wrote a function where we need to provide the VM name and the desired status that we want. The function will be running until the VM does not return the desired condition.

The code is simple, and we use it to keep retrieving the information of the VM until we get the desired state. When we get the desired state, then the function returns $True.

In the example below, we started running the function against the VM apvm002, and manually we stopped the VM. After three checks, and when the VM was deallocated, then the $True value was returned. If you want to remove the current information about the VM status, you can always remove the Else{} statement from the script.

I added the function in my GitHub, and you can access it here.

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides of the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange, CISSP and several other certifications. Anderson contributes to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at,, and Anderson (Portuguese).

Published by
Anderson Patricio

Recent Posts

Diebold Nixdorf ATMs targeted by jackpotting attacks

ATM manufacturer Diebold Nixdorf says its European machines are being hit by jackpotting attacks, where…

15 hours ago

Allow a home computer to connect to your Azure SQL server/database

In these days where remote computing has become crucial, you can connect your home computer…

19 hours ago

Migrating to Microsoft 365? Get the ball rolling with a trial tenant

Many companies still using Exchange Server are thinking of moving to Microsoft 365. You can…

22 hours ago

wpDiscuz WordPress plugin: Critical vulnerability found and patched

Users of the wpDiscuz interactive comment WordPress plugin should implement a new patch as soon…

2 days ago

Data lifecycle management: Policies and procedures for security and compliance

With the amount of electronic information consistently growing, data lifecycle management is crucial for compliance…

2 days ago

Deploy Windows from the cloud to on-premises hardware? Yes, you can

Wouldn’t it be nice if you could deploy Windows from the cloud while sipping an…

5 days ago