Use Azure PowerShell to export a template

To assist with creating Azure Resource Manager templates, you can export a template from existing resources. The exported template helps you understand the JSON syntax and properties that deploy your resources. To automate future deployments, start with the exported template and modify it for your scenario. The export template process attempts to create a usable template. However, most exported templates require some modifications before they can be used to deploy Azure resources.

Resource Manager enables you to pick one or more resources for exporting to a template. You can focus on exactly the resources you need in the template.

This article shows how to export templates through Azure PowerShell. For other options, see:

Choose the right export option

There are two ways to export a template:

  • Export from resource group or resource. This option generates a new template from existing resources. The exported template is a "snapshot" of the current state of the resource group. You can export an entire resource group or specific resources within that resource group.

  • Save from history. This option retrieves an exact copy of a template used for deployment. You specify the deployment from the deployment history.

Depending on the option you choose, the exported templates have different qualities.

From resource group or resource From history
Template is autogenerated. You'll probably want to improve the code before deploying it. Template is an exact copy of the one created by the template author. It's ready to be redeployed without any changes.
Template is snapshot of the resources' current state. It includes any manual changes you made after deployment. Template only shows state of resources at the time of deployment. Any manual changes you made after deployment aren't included.
You can select which resources from a resource group to export. All resources for a specific deployment are included. You can't pick a subset of those resources or add resources that were added at a different time.
Template includes all properties for the resources, including some properties you wouldn't normally set during deployment. You might want to remove or clean up these properties before reusing the template. Template includes only the properties needed for the deployment. The template is more sparse and easier to read.
Template probably doesn't include all of the parameters you need for reuse. Most property values are hard-coded in the template. To redeploy the template in other environments, you need to add parameters that increase the ability to configure the resources. You can unselect Include parameters so that you can author your own parameters. Template includes parameters that make it easy to redeploy in different environments.

Export the template from a resource group or resource, when:

  • You need to capture changes to the resources that were made after the original deployment.
  • You want to select which resources are exported.
  • The resources weren't created with a template.

Export the template from the history, when:

  • You want an easy-to-reuse template.
  • You don't need to include changes you made after the original deployment.

Limitations

Export is not guaranteed to succeed. Export is not a reliable way to turn pre-existing resources into templates that are usable in production. It is better to create resources from scratch using hand-written Bicep file, ARM template or terraform.

When exporting from a resource group or resource, the exported template is generated from the published schemas for each resource type. Occasionally, the schema doesn't have the latest version for a resource type. Check your exported template to make sure it includes the properties you need. If necessary, edit the exported template to use the API version you need.

Some password parameters might be missing from the exported templates. You need to check template reference, and manually add these parameters before you can use the templates to deploy resources.

The export template feature doesn't support exporting Azure Data Factory resources. To learn about how you can export Data Factory resources, see Copy or clone a data factory in Azure Data Factory.

To export resources created through classic deployment model, you must migrate them to the Resource Manager deployment model.

If you get a warning when exporting a template that indicates a resource type wasn't exported, you can still discover the properties for that resource. You can also look at the Azure REST API for the resource type.

There's a limit of 200 resources in the resource group you create the exported template for. If you attempt to export a resource group that has more than 200 resources, the error message Export template is not supported for resource groups more than 200 resources is shown.

Export template from a resource group

After setting up your resource group, you can export an Azure Resource Manager template for the resource group.

To export all resources in a resource group, use the Export-AzResourceGroup cmdlet and provide the resource group name.

Export-AzResourceGroup -ResourceGroupName demoGroup

It saves the template as a local file.

Instead of exporting all resources in the resource group, you can select which resources to export.

To export one resource, pass that resource ID.

$resource = Get-AzResource `
  -ResourceGroupName <resource-group-name> `
  -ResourceName <resource-name> `
  -ResourceType <resource-type>
Export-AzResourceGroup `
  -ResourceGroupName <resource-group-name> `
  -Resource $resource.ResourceId

To export more than one resource, pass the resource IDs in an array.

Export-AzResourceGroup `
  -ResourceGroupName <resource-group-name> `
  -Resource @($resource1.ResourceId, $resource2.ResourceId)

When exporting the template, you can specify whether parameters are used in the template. By default, parameters for resource names are included but they don't have a default value.

"parameters": {
  "serverfarms_demoHostPlan_name": {
    "type": "String"
  },
  "sites_webSite3bwt23ktvdo36_name": {
    "type": "String"
  }
}

If you use the -SkipResourceNameParameterization parameter when exporting the template, parameters for resource names aren't included in the template. Instead, the resource name is set directly on the resource to its current value. You can't customize the name during deployment.

"resources": [
  {
    "type": "Microsoft.Web/serverfarms",
    "apiVersion": "2022-09-01",
    "name": "demoHostPlan",
    ...
  }
]

If you use the -IncludeParameterDefaultValue parameter when exporting the template, the template parameter includes a default value that is set to the current value. You can either use that default value or overwrite the default value by passing in a different value.

"parameters": {
  "serverfarms_demoHostPlan_name": {
    "defaultValue": "demoHostPlan",
    "type": "String"
  },
  "sites_webSite3bwt23ktvdo36_name": {
    "defaultValue": "webSite3bwt23ktvdo36",
    "type": "String"
  }
}

Save template from deployment history

You can save a template from a deployment in the deployment history. The template you get is exactly the one that was used for deployment.

To get a template from a resource group deployment, use the Save-AzResourceGroupDeploymentTemplate cmdlet. You specify the name of the deployment to retrieve. For help with getting the name of a deployment, see View deployment history with Azure Resource Manager.

Save-AzResourceGroupDeploymentTemplate -ResourceGroupName demoGroup -DeploymentName demoDeployment

The template is saved as a local file with the name of the deployment.

To get templates deployed at other levels, use:

Next steps