How to optimize costs by setting expired date for Azure Virtual Machine by PowerShell


This script demonstrates how to set expired date for Azure Virtual Machine in Dev Labs by PowerShell.


 Related topics:

• How to create Azure Virtual Machine (VM) by Powershell using ARM API

• How to create Azure VM by Powershell using classic ASM API


Practically, developers often create Azure Virtual Machines in Dev Labs for demo or development purpose. However, those “test” VM instances are also charged. Since nobody remembers to delete them after development, company shall then pay for the instances. Thus, it must be wonderful if those “test” VM instances can be automatically deleted.

Luckily, Microsoft Azure have released new features about expired date of virtual machine on October 6, 2016. IT pro can make the use of this new feature to optimize costs. And this sample also supplies an OOB script to set expired date for your existed VM instance in Dev Labs.


• Install Azure PowerShell


• Open the script file SetExpiredDateAzureVM.ps1

• Edit the parameter “-VMName” and “-LabName” and “-ExpiredUTCDate

Set-AzureVirtualMachineExpiredDate -VMName "vmname" -LabName "labname" -ExpiredUTCDate "2016-10-10" 

Notes: the date is in UTC format


• Then run the script in PowerShell console

• After the script finishes its job, you will see similar outputs at below


Here are some code snippets for your reference.

Function Set-AzureVirtualMachineExpiredDate 
    # get vm info 
    $targetVMInfo = Get-AzureRmResource | Where { $_.Name -eq "$LabName/$VMName" -and $_.ResourceType -eq 'Microsoft.DevTestLab/labs/virtualMachines' } 
    # if not find, throw exception 
    If ($targetVMInfo -eq $null) { 
        Throw "No VM naming $VMName" 
    # get vm properties 
    $vmInfoWithProperties = Get-AzureRmResource -ResourceId $targetVMInfo.ResourceId -ExpandProperties 
    $vmProperties = $vmInfoWithProperties.Properties 
    # set expired date 
    $vmProperties | Add-Member -MemberType NoteProperty -Name expirationDate -Value $ExpiredUTCDate 
    Set-AzureRmResource -ResourceId $targetVMInfo.ResourceId -Properties $vmProperties -Force 
    Write-Host "Successfully to set VM "$LabName/$VMName" to expire on UTC $ExpiredUTCDate" 
# 1. login azure 
# 2. fill target vm name, lab name and expired date 
Set-AzureVirtualMachineExpiredDate -VMName "lab-autoexpired" -LabName "eric-lab" -ExpiredUTCDate "2016-10-10" 
Additional Resources

• Azure PowerShell SDK:

• First hand news about “expired date” of Azure Virtual Machine:


Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.