Updated – 28/07/2023 – The tool below has been updated to leverage Content Hub GA changes. The Resource Graph is being deprecated by Microsoft and replaced by the REST API.
Microsoft Sentinel comes with analytics rule built-in templates that you can turn into active analytic rules by effectively creating a copy of them – that’s what happens when you create a rule from a template.
What if you have a large number of analytics rule templates?
In this article, we will share with you how to create and enable Microsoft Sentinel Analytics Rules at scale using PowerShell, this is very useful if you have many Analytics Rule templates and you want to enable them at once, this will save thousands of mouse clicks.
Table of Contents
Introduction
Microsoft Sentinel is a cloud-native Security Information Event Management (SIEM) and Security Orchestration Automated Response (SOAR) solution. Microsoft Sentinel delivers intelligent security analytics and threat intelligence across the enterprise, providing a single solution for alert detection, threat visibility, proactive hunting, and threat response.
Microsoft Sentinel Analytics Rules templates were designed by Microsoft’s team of security experts and analysts based on known threats, common attack vectors, and suspicious activity escalation chains. Many of these templates can be customized to search for activities or filter them out, according to your needs.
Solutions in Microsoft Sentinel Content Hub provide a consolidated way to acquire Microsoft Sentinel content, like data connectors, workbooks, analytics, and automation, in your workspace with a single deployment step.
The question that comes often is, what if we have imported a solution from Content Hub that has more than 40 Analytics rules and we need to create rules from these templates?

As you probably know, this is a tedious operation to do in the Azure portal.
Let’s see how to automate this process and detect threats faster by creating and enabling the Microsoft Sentinel built-in rules at scale.
Prerequisites
To follow this article, you need to have the following:
1) Azure subscription – If you don’t have an Azure subscription, you can create a free one here.
2) Log Analytics workspace – To create a new workspace, follow the instructions here Create a Log Analytics workspace.
3) Enable Microsoft Sentinel at no additional cost on an Azure Monitor Log Analytics workspace for the first 31 days, follow the instructions here. Once Microsoft Sentinel is enabled on your Azure Monitor Log Analytics workspace, every GB of data ingested into the workspace can be retained at no charge for the first 90 days.
4) Azure PowerShell installed locally on your machine or using the Cloud Shell.
5) The Azure Resource Graph module for PowerShell. Please note that this module can be used with locally installed PowerShell, with Azure Cloud Shell, or with the PowerShell Docker image.
To install Azure Accounts PowerShell modules on your machine, you can run the following command:
# Install and update to the latest Az PowerShell module
Install-Module -Name Az.Accounts -AllowClobber -Force
# Check Az PowerShell modules version installed
Get-Module -Name Az.Accounts -ListAvailable | Select Name, Version
6) Make sure that the solution is installed from Content Hub first before you run the script described below. However, some default Analytics rules already exist without installing any solution from Content Hub. In this case, you can also run the script by targeting the Source name that you see in the Rule Templates tab under the Analytics page as shown in the figure below.

Enable Gallery Content Analytics Rules
If you are interested in enabling and activating the default built-in rules which are listed under the “galleryContent” as a source, then you need to use the native Microsoft Azure PowerShell – Microsoft Sentinel cmdlets (Az.SecurityInsights) in Windows PowerShell and PowerShell Core.
Here is an example of how you can enable the default Analytics Rules from the Gallery Content. Please note that for the solution(s) that you import from Content Hub, please refer to the next section.
#! Install Az Module If Needed
function Install-Module-If-Needed {
param([string]$ModuleName)
if (Get-Module -ListAvailable -Name $ModuleName) {
Write-Host "Module '$($ModuleName)' already exists, continue..." -ForegroundColor Green
}
else {
Write-Host "Module '$($ModuleName)' does not exist, installing..." -ForegroundColor Yellow
Install-Module $ModuleName -Force -AllowClobber -ErrorAction Stop
Write-Host "Module '$($ModuleName)' installed." -ForegroundColor Green
}
}
#! Install Az Security Insights Module If Needed
Install-Module-If-Needed Az.SecurityInsights
#! Install Az Accounts Module If Needed
Install-Module-If-Needed Az.Accounts
#! Check Azure Connection
Try {
Write-Verbose "Connecting to Azure Cloud..."
Connect-AzAccount -ErrorAction Stop | Out-Null
}
Catch {
Write-Warning "Cannot connect to Azure Cloud. Please check your credentials. Exiting!"
Break
}
#! For the Built-in Scheduled Analytics Rule Type, you can run the following command:
#! Get the list of all built-in Scheduled Analytics Rule Type
$scheduledRules = Get-AzSentinelAlertRuleTemplate -ResourceGroupName <RG-Name> -WorkspaceName <LogAnalytics-Name> | where Kind -EQ "Scheduled"
#! For the Built-in Microsoft Security Analytics Rule Type, you can run the following command:
#! Get the list of all built-in Microsoft Security Analytics Rule Type
$securityRules = Get-AzSentinelAlertRuleTemplate -ResourceGroupName <RG-Name> -WorkspaceName <LogAnalytics-Name> | where Kind -EQ "MicrosoftSecurityIncidentCreation"
#! For the Built-in Near Real-Time (NRT) Analytics Rule Type, you can run the following command:
#! Get the list of all built-in Near Real-Time (NRT) Analytics Rule Type
$nrtRules = Get-AzSentinelAlertRuleTemplate -ResourceGroupName <RG-Name> -WorkspaceName <LogAnalytics-Name> | where Kind -EQ "NRT"
#! For the Built-in Threat Intelligence Analytics Rule Type, you can run the following command:
#! Get the list of all built-in Threat Intelligence Analytics Rule Type
$tiRules = Get-AzSentinelAlertRuleTemplate -ResourceGroupName <RG-Name> -WorkspaceName <LogAnalytics-Name> | where Kind -EQ "ThreatIntelligence"
#! For the Built-in ML Behavior Analytics Rule Type, you can run the following command:
#! Get the list of all built-in ML Behavior Analytics Rule Type
$mlRules = Get-AzSentinelAlertRuleTemplate -ResourceGroupName <RG-Name> -WorkspaceName <LogAnalytics-Name> | where Kind -EQ "MLBehaviorAnalytics"
Let’s suppose you want to enable a specific built-in [Scheduled] – [Microsoft 365 Defender] Analytics Rule from the [Gallery Content], called “Multiple Teams deleted by a single user“, for example.
First, you need to identify the Alert Rule Template name which is in GUID. You run the following command to get the GUID name:
#! Get the Alert GUID for "Multiple Teams deleted by a single user"
$AlertRuleTemplateName = Get-AzSentinelAlertRuleTemplate -ResourceGroupName <RG-Name> -WorkspaceName <LogAnalytics-Name> `
| where DisplayName -eq "Multiple Teams deleted by a single user"
#! Display the GUID Name
$AlertRuleTemplateName

Then you run the following PowerShell command to create and activate the “Scheduled” rule. Note that you need to pass the rule type (Kind), the rule template name (GUID), the severity, the KQL query, the frequency, and so on.
#! Create a Scheduled Analytics Rule "Multiple Teams deleted by a single user"
New-AzSentinelAlertRule -ResourceGroupName <RG-Name> -WorkspaceName <LogAnalytics-Name> `
-Kind $AlertRuleTemplateName.Kind -AlertRuleTemplateName $AlertRuleTemplateName.Name -enabled `
-Query $AlertRuleTemplateName.Query -Severity $AlertRuleTemplateName.Severity `
-DisplayName $AlertRuleTemplateName.DisplayName -QueryFrequency $AlertRuleTemplateName.QueryFrequency `
-QueryPeriod $AlertRuleTemplateName.QueryPeriod -TriggerOperator $AlertRuleTemplateName.TriggerOperator `
-TriggerThreshold $AlertRuleTemplateName.TriggerThreshold

Of course, you can build upon it and enable multiple built-in Analytics Rules from the Gallery Content.
Please refer to the official documentation to see other examples of how to create or update the built-in Analytics Rule from the Gallery Content.
Enable Microsoft Sentinel Analytics Rules
This section will describe how to enable Microsoft Sentinel Analytics Rules for the solution(s) that you import from Content Hub.
You have a couple of options to run the script, you can either use Azure Cloud Shell, Visual Studio Code, or Windows Terminal. The Script works with PowerShell 5.1 or PowerShell 7 (core) with the Az module.
.EXAMPLE
.\Set-AnalyticsRules -SubscriptionId "xxxxxxxx-aaaa-bbbb-cccc-zzzzzzzzzzzz" -ResourceGroup "RG-Name" `
-WorkspaceName "Log-Analytics-Name" -SolutionName "Source-Name" -enableRules [Yes] -Verbose
This example will connect to your Azure account using the subscription Id specified, and then create all analytics rules from templates for the specified Microsoft Sentinel solution name (E.g. SAP, Cloudflare, Dynamics 365, Azure Activity, or Azure Active Directory).
By default, all of the rules will be created in a Disabled state, however, you have the option to enable the rules at creation time as well by setting the parameter (enable rules) to Yes.
Here is an example of the output once you run this tool:

PowerShell Code
Updated – 28/07/2023 – The tool below has been updated to leverage Content Hub GA changes. The Resource Graph is being deprecated by Microsoft and replaced by the REST API.
The complete script is detailed below to automate the entire creation process of Analytics Rules:
<#
.SYNOPSIS
Enable Microsoft Sentinel Analytics Rules at Scale.
.DESCRIPTION
How to create and enable Microsoft Sentinel Analytics Rules at Scale using PowerShell.
.NOTES
File Name : Set-AnalyticsRules.ps1
Author : Microsoft MVP/MCT - Charbel Nemnom
Version : 2.0
Date : 24-October-2022
Update : 28-July-2023
Requires : PowerShell 5.1 or PowerShell 7.3.x (Core)
Module : Az Module
.LINK
To provide feedback or for further assistance please visit:
https://charbelnemnom.com
.EXAMPLE
.\Set-AnalyticsRules.ps1 -SubscriptionId <SUB-ID> -ResourceGroup <RG-Name> -WorkspaceName <Log-Analytics-Name> -SolutionName <Source-Name> -enableRules [Yes] -Verbose
This example will connect to your Azure account using the subscription Id specified, and then create all analytics rules from templates for the specified Microsoft Sentinel solution.
By default, all of the rules will be created in a Disabled state, however, you have the option to enable the rules at creation time by setting the parameter -enableRules [Yes].
#>
param (
[Parameter(Position = 0, Mandatory = $true, HelpMessage = 'Enter Azure Subscription ID')]
[string]$subscriptionId,
[Parameter(Position = 1, Mandatory = $true, HelpMessage = 'Enter Resource Group Name where Microsoft Sentinel is deployed')]
[string]$resourceGroupName,
[Parameter(Position = 2, Mandatory = $true, HelpMessage = 'Enter Log Analytics Workspace Name')]
[string]$workspaceName,
[Parameter(Position = 3, Mandatory = $true, HelpMessage = 'Enter Microsoft Sentinel Solution Name')]
[string]$solutionName,
[ValidateSet("Yes", "No")]
[String]$enableRules = 'No'
)
#! Install Az Module If Needed
function Install-Module-If-Needed {
param([string]$ModuleName)
if (Get-Module -ListAvailable -Name $ModuleName) {
Write-Host "Module '$($ModuleName)' already exists, continue..." -ForegroundColor Green
}
else {
Write-Host "Module '$($ModuleName)' does not exist, installing..." -ForegroundColor Yellow
Install-Module $ModuleName -Force -AllowClobber -ErrorAction Stop
Write-Host "Module '$($ModuleName)' installed." -ForegroundColor Green
}
}
#! Install Az Accounts Module If Needed
Install-Module-If-Needed Az.Accounts
#! Check Azure Connection
Try {
Write-Verbose "Connecting to Azure Cloud..."
Connect-AzAccount -ErrorAction Stop | Out-Null
}
Catch {
Write-Warning "Cannot connect to Azure Cloud. Please check your credentials. Exiting!"
Break
}
#! Get Az Access Token
$token = Get-AzAccessToken #This will default to Azure Resource Manager endpoint
$authHeader = @{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer ' + $token.Token
}
$URI = "https://management.azure.com/subscriptions/$subscriptionid/resourceGroups/$resourceGroupName/providers/Microsoft.OperationalInsights/workspaces/$workspaceName/providers/Microsoft.SecurityInsights/contentTemplates?api-version=2023-06-01-preview"
$response = Invoke-RestMethod $URI -Method 'GET' -Headers $authHeader
$solution = $response.value
try {
$ruleTemplates = $solution | Where-Object { $_.properties.mainTemplate.resources.properties.source.name -eq "$solutionName" -and $_.properties.contentKind -eq "AnalyticsRule" }
if ($ruleTemplates.Count -eq 0) {
throw "Solution Name: [$solutionName] cannot be found. Please check the solution name and Install it from the Content Hub blade"
}
}
catch {
Write-Error $_ -ErrorAction Stop
}
Write-Verbose "$($ruleTemplates.count) Analytic Rules found for: [$solutionName]"
foreach ($ruleTemplate in $ruleTemplates) {
$ruleId = $ruleTemplate.Properties.contentId
$rule = $ruleTemplate.properties.mainTemplate.resources | Where-Object type -eq 'Microsoft.SecurityInsights/AlertRuleTemplates' | Select-Object kind, properties
$rule.properties | Add-Member -NotePropertyName alertRuleTemplateName -NotePropertyValue $ruleId
$rule.properties | Add-Member -NotePropertyName templateVersion -NotePropertyValue $template.version
If ($enableRules -eq "Yes") {
$rule.properties.enabled = $true
}
$payload = $rule | ConvertTo-Json -Depth 100
$apiPath = "/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.OperationalInsights/workspaces/$workspaceName/providers/Microsoft.SecurityInsights/alertRules/$($ruleId)?api-version=2023-06-01-preview"
try {
$result = Invoke-AzRestMethod -Method PUT -path $apiPath -Payload $payload
If ($enableRules -eq "Yes") {
Write-Verbose "Creating and Enabling rule $($rule.properties.displayName)"
}
Else {
Write-Verbose "Creating rule $($rule.properties.displayName)"
}
if (!($result.StatusCode -in 200, 201)) {
Write-Host $result.StatusCode
Write-Host $result.Content
throw "Error when enabling Analytics rule $($rule.properties.displayName)"
}
}
catch {
Write-Error $_ -ErrorAction Continue
}
}
Before running the tool, we can see that we don’t have any active rules created in the Disabled state. In this example, we have 15 Active rules only.

Once you run this tool, you can refresh the Analytics rules page and check all the rules are created in the Disabled state. In this example, we have 58 Active rules.

That’s it there you have it. Happy Analytics Rules creation with Microsoft Sentinel!
If you have any feedback or changes that everyone should receive, please feel free to leave a comment below.
Summary
This article showed you how to create and enable Microsoft Sentinel Analytics Rules at scale using PowerShell, this is very useful if you have many Analytics Rule templates as part of a Content Hub solution and you want to enable them at once.
Analytics rules search for specific events or sets of events across your environment, alert you when certain event thresholds or conditions are reached, generate incidents for your SOC to triage and investigate, and respond to threats with automated tracking and remediation processes.
This is one of the many features in Microsoft Sentinel that can be utilized to provide immense value to threat detection out-of-the-box.
The power of Microsoft Sentinel comes from the ability to detect, investigate, respond to, and remediate threats.
__
Thank you for reading my blog.
If you have any questions or feedback, please leave a comment.
-Charbel Nemnom-
Hello Matej, thanks for the comment!
Once you activate and create Rules from the “Rule Templates” Tab, you will see either source name set as “Repositories” or “Gallery Content” in the “Active Rules” Tab, and there is also a third option called “Custom Content” for the custom rules that you create.
I have updated the article to include both Options (Solutions that you imported from Content Hub and the default built-in Analytics Rules in the Gallery Content).
Please check this section and let me know if it works for you.
Thanks!
Works like a charm! This is exactly what I was trying to do. Thank you, you are awesome.
Thank you, Matej for the confirmation! I am happy to hear that.
Hello Charbel,
I have installed the Azure Active Directory content and am running the script from PowerShell as administrator while authenticating with an account which has owner rights on the subscription and resource group. Still however, I receive the Resource Graph query error. Do you have an idea what I am missing?
Thank you
Hello KB, thanks for the comment and for reporting this!
Yes, I faced the same error, this is because Microsoft just announced Content Hub general availability (GA) and Out-of-the-box (OOTB) Content Centralization.
For this to work, please go to your Content Hub and then Delete Azure Active Directory solution all together.
Wait for few minutes… and then install the latest Azure Active Directory version 2.0.13 from Content Hub blade again.
I just tried it and I was able to activate all 59 Analytics Rule Templates for Azure Active Directory.
Hope it helps!
Hi,
Thank you for creating the blog. I was trying to run the script after installing the Azure Active Directory solution but I have been greeted with the same error message as some of the others: “VERBOSE: Sent top=100 skip=0 skipToken=
VERBOSE: Received results: 0
Exception: Resource Graph query error”
I have checked my permissions and I have Owner and Global admin on the subscription in which the sentinel and the LA workspace resides which is inherited.
I have tried uninstalling and installing the modules.
I have tried reinstalling the solution on the content hub as you suggested.
Could you please help me with what the issue could be?
Hello Marcell, thanks for the comment and feedback!
As I mentioned in one of my previous comment, please go to your Content Hub and then “Delete” Azure Active Directory solution all together, and then “wait for few minutes” after the deletion.
Then install the latest Azure Active Directory version from Content Hub blade again, and then “wait for few minutes” after the installation.
Last, try to run the script again and activate the Analytics Rule Templates for Azure Active Directory solution.
Hope it helps!
Hi,
Thank you for the quick response. I have taken the steps you have recommended, and it has resulted in the same output.
I have used the following query in the relevant subscription using Azure Resource Graph Explorer to investigate why the Received results are 0 when running the script.
resources
| where type =~ ‘Microsoft.Resources/templateSpecs/versions’
| where tags[‘hidden-sentinelContentType’] =~ ‘AnalyticsRule’
To my surprise, the query has not returned any results.
When I navigate to the Analytics > Rule templates > sourcename: Azure Active Directory – I can see all 59 of the Analytic rules that I would like to deploy with the script.
Do you have any advice for me?
Hi Charbel, thanks for your work.
I tried your script but i got the same error as above “Resource Graph query error”.
I tried what you said with uninstall / reinstall the content but same result.
So i looked inside the script to understand what is going on, and i saw that on a tenant i got these type of resource :
(Search-AzGraph -Subscription “xxxxxxx” -Query “resources”).type
microsoft.storage/storageaccounts
microsoft.insights/workbooks
microsoft.logic/workflows
microsoft.managedidentity/userassignedidentities
microsoft.operationalinsights/workspaces
microsoft.operationsmanagement/solutions
microsoft.web/connections
microsoft.web/connections
microsoft.web/connections
And no ‘Microsoft.Resources/templateSpecs/versions’ that we are looking for.
The strange part is that on GUI in sentinel instance, we have the AAD connector setup in content hub, with all analytics rules template.
On an other tenant, with same deployment type I got the resources that we are looking for and it’s working as intend…
Do you have an idea?
Hello Marcell, thanks for confirming!
After deep investigation, I checked with Microsoft, unfortunately, the Resource Graph is being deprecated.
If you want to see the rules that were created, you need to use the REST API.
I’ll update the article in the next few days and switch over to the new method.
Hello Ted, thanks for reporting this!
After deep investigation, I checked with Microsoft, unfortunately, the Resource Graph is being deprecated for Sentinel.
If you want to see the rules that were created, you need to use the REST API.
I’ll update the article in the next few days and switch over to the new method.
Hello everyone, please note that the tool has been updated to take into account the new changes that has been implemented by Microsoft.
The Resource Graph for Microsoft Sentinel is being deprecated and replaced by the REST API.
The updated tool is working beautifully. Enjoy :)