Automate Application Insights with PowerShell - Azure Monitor (2023)

  • Article
  • 9 minutes to read


We recommend that you use the Azure Az PowerShell module to interact with Azure. See Install Azure PowerShell to get started. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

This article shows you how to automate the creation and update of Application Insights resources automatically by using Azure Resource Manager. You might, for example, do so as part of a build process. Along with the basic Application Insights resource, you can create availability web tests, set up alerts, set the pricing scheme, and create other Azure resources.

The key to creating these resources is JSON templates for Resource Manager. The basic procedure is:

  • Download the JSON definitions of existing resources.
  • Parameterize certain values, such as names.
  • Run the template whenever you want to create a new resource.

You can package several resources together to create them all in one go. For example, you can create an app monitor with availability tests, alerts, and storage for continuous export. There are some subtleties to some of the parameterizations, which we'll explain here.

One-time setup

If you haven't used PowerShell with your Azure subscription before, install the Azure PowerShell module on the machine where you want to run the scripts:

  1. Install Microsoft Web Platform Installer (v5 or higher).
  2. Use it to install Azure PowerShell.

In addition to using Azure Resource Manager templates (ARM templates), there's a rich set of Application Insights PowerShell cmdlets. These cmdlets make it easy to configure Application Insights resources programatically. You can use the capabilities enabled by the cmdlets to:

  • Create and delete Application Insights resources.
  • Get lists of Application Insights resources and their properties.
  • Create and manage continuous export.
  • Create and manage application keys.
  • Set the daily cap.
  • Set the pricing plan.

Create Application Insights resources by using a PowerShell cmdlet

Here's how to create a new Application Insights resource in the Azure East US datacenter by using the New-AzApplicationInsights cmdlet:

New-AzApplicationInsights -ResourceGroupName <resource group> -Name <resource name> -location eastus

Create Application Insights resources by using an ARM template

Here's how to create a new Application Insights resource by using an ARM template.

(Video) How to monitor app performance with Azure Monitor Application Insights

Create the ARM template

Create a new .json file. Let's call it template1.json in this example. Copy this content into it:

 { "$schema": "", "contentVersion": "", "parameters": { "appName": { "type": "string", "metadata": { "description": "Enter the name of your Application Insights resource." } }, "appType": { "type": "string", "defaultValue": "web", "allowedValues": [ "web", "java", "other" ], "metadata": { "description": "Enter the type of the monitored application." } }, "appLocation": { "type": "string", "defaultValue": "eastus", "metadata": { "description": "Enter the location of your Application Insights resource." } }, "retentionInDays": { "type": "int", "defaultValue": 90, "allowedValues": [ 30, 60, 90, 120, 180, 270, 365, 550, 730 ], "metadata": { "description": "Data retention in days" } }, "ImmediatePurgeDataOn30Days": { "type": "bool", "defaultValue": false, "metadata": { "description": "If set to true when changing retention to 30 days, older data will be immediately deleted. Use this with extreme caution. This only applies when retention is being set to 30 days." } }, "priceCode": { "type": "int", "defaultValue": 1, "allowedValues": [ 1, 2 ], "metadata": { "description": "Pricing plan: 1 = Per GB (or legacy Basic plan), 2 = Per Node (legacy Enterprise plan)" } }, "dailyQuota": { "type": "int", "defaultValue": 100, "minValue": 1, "metadata": { "description": "Enter daily quota in GB." } }, "dailyQuotaResetTime": { "type": "int", "defaultValue": 0, "metadata": { "description": "Enter daily quota reset hour in UTC (0 to 23). Values outside the range will get a random reset hour." } }, "warningThreshold": { "type": "int", "defaultValue": 90, "minValue": 1, "maxValue": 100, "metadata": { "description": "Enter the % value of daily quota after which warning mail to be sent. " } } }, "variables": { "priceArray": [ "Basic", "Application Insights Enterprise" ], "pricePlan": "[take(variables('priceArray'),parameters('priceCode'))]", "billingplan": "[concat(parameters('appName'),'/', variables('pricePlan')[0])]" }, "resources": [ { "type": "microsoft.insights/components", "kind": "[parameters('appType')]", "name": "[parameters('appName')]", "apiVersion": "2014-04-01", "location": "[parameters('appLocation')]", "tags": {}, "properties": { "ApplicationId": "[parameters('appName')]", "retentionInDays": "[parameters('retentionInDays')]", "ImmediatePurgeDataOn30Days": "[parameters('ImmediatePurgeDataOn30Days')]" }, "dependsOn": [] }, { "name": "[variables('billingplan')]", "type": "microsoft.insights/components/CurrentBillingFeatures", "location": "[parameters('appLocation')]", "apiVersion": "2015-05-01", "dependsOn": [ "[resourceId('microsoft.insights/components', parameters('appName'))]" ], "properties": { "CurrentBillingFeatures": "[variables('pricePlan')]", "DataVolumeCap": { "Cap": "[parameters('dailyQuota')]", "WarningThreshold": "[parameters('warningThreshold')]", "ResetTime": "[parameters('dailyQuotaResetTime')]" } } } ] }

Use the ARM template to create a new Application Insights resource

  1. In PowerShell, sign in to Azure by using $Connect-AzAccount.

  2. Set your context to a subscription with Set-AzContext "<subscription ID>".

  3. Run a new deployment to create a new Application Insights resource:

     New-AzResourceGroupDeployment -ResourceGroupName Fabrikam ` -TemplateFile .\template1.json ` -appName myNewApp
    • -ResourceGroupName is the group where you want to create the new resources.
    • -TemplateFile must occur before the custom parameters.
    • -appName is the name of the resource to create.

You can add other parameters. You'll find their descriptions in the parameters section of the template.

Get the instrumentation key

After you create an application resource, you'll want the instrumentation key:

  1. Sign in to Azure by using $Connect-AzAccount.
  2. Set your context to a subscription with Set-AzContext "<subscription ID>".
  3. Then use:
    1. $resource = Get-AzResource -Name "<resource name>" -ResourceType "Microsoft.Insights/components"
    2. $details = Get-AzResource -ResourceId $resource.ResourceId
    3. $details.Properties.InstrumentationKey

To see a list of many other properties of your Application Insights resource, use:

Get-AzApplicationInsights -ResourceGroupName Fabrikam -Name FabrikamProd | Format-List

More properties are available via the cmdlets:

  • Set-AzApplicationInsightsDailyCap
  • Set-AzApplicationInsightsPricingPlan
  • Get-AzApplicationInsightsApiKey
  • Get-AzApplicationInsightsContinuousExport

See the detailed documentation for the parameters for these cmdlets.


On March 31, 2025, support for instrumentation key ingestion will end. Instrumentation key ingestion will continue to work, but we'll no longer provide updates or support for the feature. Transition to connection strings to take advantage of new capabilities.

(Video) Azure Application Insights Tutorial | Amazing telemetry service

Set the data retention

You can use the following three methods to programmatically set the data retention on an Application Insights resource.

Set data retention by using PowerShell commands

Here's a simple set of PowerShell commands to set the data retention for your Application Insights resource:

$Resource = Get-AzResource -ResourceType Microsoft.Insights/components -ResourceGroupName MyResourceGroupName -ResourceName MyResourceName$Resource.Properties.RetentionInDays = 365$Resource | Set-AzResource -Force

Set data retention by using REST

To get the current data retention for your Application Insights resource, you can use the OSS tool ARMClient. Learn more about ARMClient from articles by David Ebbo and Daniel Bowbyes. Here's an example that uses ARMClient to get the current retention:

armclient GET /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName?api-version=2018-05-01-preview

To set the retention, the command is a similar PUT:

armclient PUT /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName?api-version=2018-05-01-preview "{location: 'eastus', properties: {'retentionInDays': 365}}"

To set the data retention to 365 days by using the preceding template, run:

New-AzResourceGroupDeployment -ResourceGroupName "<resource group>" ` -TemplateFile .\template1.json ` -retentionInDays 365 ` -appName myApp

Set data retention by using a PowerShell script

The following script can also be used to change retention. Copy this script to save it as Set-ApplicationInsightsRetention.ps1.

Param( [Parameter(Mandatory = $True)] [string]$SubscriptionId, [Parameter(Mandatory = $True)] [string]$ResourceGroupName, [Parameter(Mandatory = $True)] [string]$Name, [Parameter(Mandatory = $True)] [string]$RetentionInDays)$ErrorActionPreference = 'Stop'if (-not (Get-Module Az.Accounts)) { Import-Module Az.Accounts}$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profileif (-not $azProfile.Accounts.Count) { Write-Error "Ensure you have logged in before calling this function." }$currentAzureContext = Get-AzContext$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azProfile)$token = $profileClient.AcquireAccessToken($currentAzureContext.Tenant.TenantId)$UserToken = $token.AccessToken$RequestUri = "$($SubscriptionId)/resourceGroups/$($ResourceGroupName)/providers/Microsoft.Insights/components/$($Name)?api-version=2015-05-01"$Headers = @{ "Authorization" = "Bearer $UserToken" "x-ms-client-tenant-id" = $currentAzureContext.Tenant.TenantId}## Get Component object via ARM$GetResponse = Invoke-RestMethod -Method "GET" -Uri $RequestUri -Headers $Headers ## Update RetentionInDays propertyif($($ | Get-Member "RetentionInDays")){ $ = $RetentionInDays}else{ $ | Add-Member -Type NoteProperty -Name "RetentionInDays" -Value $RetentionInDays}## Upsert Component object via ARM$PutResponse = Invoke-RestMethod -Method "PUT" -Uri "$($RequestUri)" -Headers $Headers -Body $($GetResponse | ConvertTo-Json) -ContentType "application/json"$PutResponse

This script can then be used as:

Set-ApplicationInsightsRetention ` [-SubscriptionId] <String> ` [-ResourceGroupName] <String> ` [-Name] <String> ` [-RetentionInDays <Int>]

Set the daily cap

To get the daily cap properties, use the Set-AzApplicationInsightsPricingPlan cmdlet:

Set-AzApplicationInsightsDailyCap -ResourceGroupName <resource group> -Name <resource name> | Format-List

To set the daily cap properties, use the same cmdlet. For instance, to set the cap to 300 GB per day:

Set-AzApplicationInsightsDailyCap -ResourceGroupName <resource group> -Name <resource name> -DailyCapGB 300

You can also use ARMClient to get and set daily cap parameters. To get the current values, use:

armclient GET /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview

Set the daily cap reset time

To set the daily cap reset time, you can use ARMClient. Here's an example using ARMClient to set the reset time to a new hour. This example shows 12:00 UTC:

armclient PUT /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview "{'CurrentBillingFeatures':['Basic'],'DataVolumeCap':{'Cap':100,'WarningThreshold':80,'ResetTime':12}}"

Set the pricing plan

To get the current pricing plan, use the Set-AzApplicationInsightsPricingPlan cmdlet:

(Video) Using Application Insights to Log PowerShell Scripts

Set-AzApplicationInsightsPricingPlan -ResourceGroupName <resource group> -Name <resource name> | Format-List

To set the pricing plan, use the same cmdlet with the -PricingPlan specified:

Set-AzApplicationInsightsPricingPlan -ResourceGroupName <resource group> -Name <resource name> -PricingPlan Basic

You can also set the pricing plan on an existing Application Insights resource by using the preceding ARM template, omitting the "microsoft.insights/components" resource and the dependsOn node from the billing resource. For instance, to set it to the Per GB plan (formerly called the Basic plan), run:

 New-AzResourceGroupDeployment -ResourceGroupName "<resource group>" ` -TemplateFile .\template1.json ` -priceCode 1 ` -appName myApp

The priceCode is defined as:

1Per GB (formerly named the Basic plan)
2Per Node (formerly name the Enterprise plan)

Finally, you can use ARMClient to get and set pricing plans and daily cap parameters. To get the current values, use:

armclient GET /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview

You can set all of these parameters by using:

armclient PUT /subscriptions/00000000-0000-0000-0000-00000000000/resourceGroups/MyResourceGroupName/providers/microsoft.insights/components/MyResourceName/CurrentBillingFeatures?api-version=2018-05-01-preview"{'CurrentBillingFeatures':['Basic'],'DataVolumeCap':{'Cap':200,'ResetTime':12,'StopSendNotificationWhenHitCap':true,'WarningThreshold':90,'StopSendNotificationWhenHitThreshold':true}}"

This code will set the daily cap to 200 GB per day, configure the daily cap reset time to 12:00 UTC, send emails both when the cap is hit and the warning level is met, and set the warning threshold to 90% of the cap.

Add a metric alert

To automate the creation of metric alerts, see the Metric alerts template article.

Add an availability test

To automate availability tests, see the Metric alerts template article.

Add more resources

To automate the creation of any other resource of any kind, create an example manually and then copy and parameterize its code from Azure Resource Manager.

  1. Open Azure Resource Manager. Navigate down through subscriptions/resourceGroups/<your resource group>/providers/Microsoft.Insights/components to your application resource.

    Automate Application Insights with PowerShell - Azure Monitor (1)

    Components are the basic Application Insights resources for displaying applications. There are separate resources for the associated alert rules and availability web tests.

    (Video) What to use for monitoring your applications in Azure | Azure Friday

  2. Copy the JSON of the component into the appropriate place in template1.json.

  3. Delete these properties:

    • id
    • InstrumentationKey
    • CreationDate
    • TenantId
  4. Open the webtests and alertrules sections and copy the JSON for individual items into your template. Don't copy from the webtests or alertrules nodes. Go into the items under them.

    Each web test has an associated alert rule, so you have to copy both of them.

  5. Insert this line in each resource:

    "apiVersion": "2015-05-01",

Parameterize the template

Now you have to replace the specific names with parameters. To parameterize a template, you write expressions using a set of helper functions.

You can't parameterize only part of a string, so use concat() to build strings.

Here are examples of the substitutions you'll want to make. There are several occurrences of each substitution. You might need others in your template. These examples use the parameters and variables we defined at the top of the template.

FindReplace with
"/subscriptions/.../../alertrules/myAlertName-myAppName-subsId","[resourceId('Microsoft.Insights/alertrules', variables('alertRuleName'))]",
"/subscriptions/.../../webtests/myTestName-myAppName","[resourceId('Microsoft.Insights/webtests', parameters('webTestName'))]",
"myappname" (lower case)"[toLower(parameters('appName'))]"
"<WebTest Name=\"myWebTest\" ...
Url=\"\" ...>"
[concat('<WebTest Name=\"',
'\" ... Url=\"', parameters('Url'),

Set dependencies between the resources

Azure should set up the resources in strict order. To make sure one setup completes before the next begins, add dependency lines:

  • In the availability test resource:

    "dependsOn": ["[resourceId('Microsoft.Insights/components', parameters('appName'))]"],

    (Video) Use Azure Monitor to analyze logs and metrics for your Node web app

  • In the alert resource for an availability test:

    "dependsOn": ["[resourceId('Microsoft.Insights/webtests', variables('testName'))]"],

Next steps

See these other automation articles:

  • Create an Application Insights resource via a quick method without using a template.
  • Create web tests.
  • Send Azure Diagnostics to Application Insights.
  • Create release annotations.


What are the limitations of Azure monitor? ›

The following limits apply to the Azure Monitor workspace ingesting your Prometheus metrics. Active time series with metrics that have been reported in the last ~12 hours. You can request an increase. You can request an increase.
Query pre-parsing limits.
Query hours per Azure tenant600,000
2 more rows
Oct 12, 2022

What is the difference between application Insights and Azure monitor? ›

Application Insights is an extension of Azure Monitor and provides Application Performance Monitoring (also known as “APM”) features. APM tools are useful to monitor applications from development, through test, and into production in the following ways: Proactively understand how an application is performing.

Which of the following application monitoring tool can monitor an already deployed Azure web app without redeploying it? ›

Install the Status Monitor on your existing Azure App Services and virtual machines through the Azure portal to get performance monitoring without needing to update and redeploy your application. Visual Studio Application Insights is an extensible analytics service that monitors your live web application.

Can Azure monitor monitor the performance of on-premises? ›

Can Azure Monitor monitor on-premises resources? Yes, in addition to collecting monitoring data from Azure resources, Azure Monitor can collect data from virtual machines and applications in other clouds and on-premises.

What are the three main functions of Azure Monitor? ›

Microsoft combined three unique services—Azure Monitor, Log Analytics, and Application Insights—under the umbrella of Azure Monitor to provide powerful end-to-end monitoring of your applications and the components they rely on. Log Analytics and Application Insights are now features of Azure Monitor.

What are the disadvantages of monitoring? ›

Though it can be done successfully, there are some disadvantages associated with employee monitoring:
  • Ethics and Feelings of Distrust.
  • Cost in Time and Resources.
  • Legal Implications and Risks.
  • Increased Productivity.
  • Improved Team Performance.
  • Cost-Effectiveness.
  • Increased Internal Security and Protection from Harassment.
Jan 31, 2021


1. Full-stack end-to-end monitoring with Azure Monitor | Azure Friday
(Microsoft Developer)
2. How to use Azure Monitor to observe and diagnose a JavaScript error
(Microsoft Azure)
3. Azure Monitor with Automations
4. How to perform usage analysis with Azure Monitor Application Insights
(Microsoft Azure)
5. Monitoring and Troubleshooting with Azure Application Insights
(Cloud Lunch and Learn)
6. Application Insights - Custom Availability Tests with the Common Data Service API
(Ali Youssefi)
Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated: 04/26/2023

Views: 6143

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.