| title | Use commands to start and stop lab VMs |
|---|---|
| description | Use Azure PowerShell or Azure CLI command lines and scripts to start and stop Azure DevTest Labs virtual machines (VMs). |
| ms.topic | how-to |
| ms.author | rosemalcolm |
| author | RoseHJM |
| ms.date | 03/27/2025 |
| ms.custom | devx-track-azurepowershell, devx-track-azurecli, UpdateFrequency2 |
| ms.devlang | azurecli |
This article shows how you can use PowerShell or Azure CLI commands to script or automate start or stop for Azure DevTest Labs VMs. For example, you can use start or stop commands to:
- Test a three-tier application where the tiers need to start in a sequence.
- Turn off your VMs to save costs when they meet custom criteria.
- Start and stop a VM when a continuous integration and continuous delivery (CI/CD) workflow begins and finishes.
Note
You can also start, stop, or restart DevTest Labs VMs by using the Azure portal. Lab admins can use the portal to configure automatic startup and automatic shutdown schedules and policies for lab VMs.
- Admin access to a lab VM in DevTest Labs.
- Access to Azure PowerShell. You can use the Azure Cloud Shell PowerShell environment, or install Azure PowerShell to use a physical or virtual machine. If necessary, run
Update-Module -Name Azto update your installation.
- Admin access to a lab VM in DevTest Labs.
- Access to Azure CLI. You can use the Azure Cloud Shell Bash environment, or install Azure CLI to use a physical or virtual machine with a Bash or Windows environment.
The following PowerShell script starts or stops a VM in a lab by using the Invoke-AzResourceAction PowerShell cmdlet. The ResourceId parameter is the fully qualified ID for the lab VM you want to start or stop. The Action parameter determines whether to start or stop the VM, depending on which action you need.
-
If you use Cloud Shell, make sure the PowerShell environment is selected.
-
Use the PowerShell Connect-AzAccount cmdlet to sign in to your Azure account. If you have multiple Azure subscriptions, uncomment
Set-AzContextand provide the<SubscriptionId>you want to use.$sub = Get-AzSubscription -ErrorAction SilentlyContinue if(-not($sub)) { Connect-AzAccount } # Set-AzContext -SubscriptionId "<Subscription ID>"
-
Set variables by providing your own values for
<lab name>,<VM name>, and whether toStartorStopthe VM.$devTestLabName = "<lab name>" $vMToStart = "<VM name>" $vmAction = "<Start or Stop>"
-
Start or stop the VM, based on the value you passed to
$vmAction.# Get the lab information $devTestLab = Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs' -ResourceName $devTestLabName # Start or stop the VM and return a succeeded or failed status $returnStatus = Invoke-AzResourceAction ` -ResourceId "$($devTestLab.ResourceId)/virtualmachines/$vMToStart" ` -Action $vmAction ` -Force if ($returnStatus.Status -eq 'Succeeded') { Write-Output "##[section] Successfully updated DTL machine: $vMToStart, Action: $vmAction" } else { Write-Error "##[error] Failed to update DTL machine: $vMToStart, Action: $vmAction" }
The following script uses the Azure CLI az lab vm start or az lab vm stop command to start or stop a lab VM.
To run the script locally, use the appropriate syntax depending on whether you have a Bash or Windows environment. In Cloud Shell, use the Bash environment and syntax.
-
Sign in to your Azure account. If you have multiple Azure subscriptions, uncomment the
az account setline and provide a subscription ID to use.az login REM az account set --subscription <SubscriptionId> -
If you don't know the name of the Azure resource group that contains your lab, find it by providing your
<lab name>in the following query.az resource list --resource-type "Microsoft.DevTestLab/labs" --name "<lab name>" --query "[0].resourceGroup" -
Set variables by providing values for
<SubscriptionId>,<resourceGroup>,<lab name>,<VM name>, and whether toStartorStopthe VM.Bash
SUBSCRIPTIONID=<SubscriptionId> RESOURCEGROUP=<resourceGroup> DEVTESTLABNAME=<lab name> VMNAME=<VM name> ACTION=<Start or Stop>Windows
set SUBSCRIPTIONID=<SubscriptionId> set RESOURCEGROUP=<resourceGroup> set DEVTESTLABNAME=<lab name> set VMNAME=<VM name> set ACTION=<Start or Stop> -
Run the following Azure CLI command to start or stop the VM, based on the value passed to
ACTION.Bash
az lab vm $ACTION --lab-name $DEVTESTLABNAME --name $VMNAME --resource-group $RESOURCEGROUPWindows
az lab vm %ACTION% --lab-name %DEVTESTLABNAME% --name %VMNAME% --resource-group %RESOURCEGROUP%