Use PowerShell to set allowed VM sizes in Azure Lab Services

This sample PowerShell script sets allowed virtual machine (VM) sizes in Azure Lab Services.


This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.

This sample requires Azure PowerShell. Run Get-Module -ListAvailable Az to find the version. If you need to install or upgrade, see Install Azure PowerShell module.

Run the Connect-AzAccount cmdlet to connect to Azure.


  • A lab. The script requires you to have an existing lab.

Sample script

    [Parameter(Mandatory=$true, HelpMessage="The name of the DevTest Lab to update")]
    [string] $DevTestLabName,

    [Parameter(Mandatory=$true, HelpMessage="The array of VM Sizes to be added")]
    [Array] $SizesToAdd

function Get-Lab
    $lab = Find-AzResource -ResourceType 'Microsoft.DevTestLab/labs' -ResourceNameEquals $DevTestLabName

        throw "Lab named $DevTestLabName was not found"
    return $lab

function Get-PolicyChanges ($lab)
    #start by finding the existing policy
    $script:labResourceName = $lab.Name + '/default'
    $existingPolicy = (Get-AzResource -ResourceType 'Microsoft.DevTestLab/labs/policySets/policies' -ResourceName $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2016-05-15) | Where-Object {$_.Name -eq 'AllowedVmSizesInLab'}
        $existingSizes = $existingPolicy.Properties.threshold
        $savePolicyChanges = $false
        $existingSizes = ''
        $savePolicyChanges = $true

    if($existingPolicy.Properties.threshold -eq '[]')
        Write-Output "Skipping $($lab.Name) because it currently allows all sizes"

    # Make a list of all the sizes. It needs all their current sizes as well as any from our list that arent already there
    $finalVmSizes = $existingSizes.Replace('[', '').Replace(']', '').Split(',',[System.StringSplitOptions]::RemoveEmptyEntries)

    foreach($vmSize in $SizesToAdd)
        $quotedSize = '"' + $vmSize + '"'

            $finalVmSizes += $quotedSize
            $savePolicyChanges = $true

        Write-Output "No policy changes required for VMSize in lab $($lab.Name)"

    return @{
        existingPolicy = $existingPolicy
        savePolicyChanges = $savePolicyChanges
        finalVmSizes = $finalVmSizes

function Set-PolicyChanges ($lab, $policyChanges)
        $thresholdValue = ('[' + [String]::Join(',', $policyChanges.finalVmSizes) + ']')

        $policyObj = @{
            subscriptionId = $lab.SubscriptionId
            status = 'Enabled'
            factName = 'LabVmSize'
            resourceGroupName = $lab.ResourceGroupName
            labName = $lab.Name
            policySetName = 'default'
            name = $lab.Name + '/default/allowedvmsizesinlab'
            threshold = $thresholdValue
            evaluatorType = 'AllowedValuesPolicy'

        $resourceType = "Microsoft.DevTestLab/labs/policySets/policies/AllowedVmSizesInLab"
            Write-Output "Updating $($lab.Name) VM Size policy"
            Set-AzResource -ResourceType $resourceType -ResourceName $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2016-05-15 -Properties $policyObj -Force
            Write-Output "Creating $($lab.Name) VM Size policy"
            New-AzResource -ResourceType $resourceType -ResourceName $labResourceName -ResourceGroupName $lab.ResourceGroupName -ApiVersion 2016-05-15 -Properties $policyObj -Force

$lab = Get-Lab
$policyChanges = Get-PolicyChanges $lab
Set-PolicyChanges $lab $policyChanges

Script explanation

This script uses the following commands:

Command Notes
Find-AzResource Searches for resources based on specified parameters.
Get-AzResource Gets resources.
Set-AzResource Modifies a resource.
New-AzResource Create a resource.

Next steps

For more information on the Azure PowerShell, see Azure PowerShell documentation.

Additional Azure Lab Services PowerShell script samples can be found in the Azure Lab Services PowerShell samples.