Enable Audit History Using Powershell in D365


Microsoft Dynamics CRM has a useful Auditing feature that helps organizations keep an eye on any changes made to their data. This is important for keeping the data accurate, following rules, and making things more secure. In this article, we'll learn how to use PowerShell to turn on this auditing feature in Dynamics CRM, making it easier for administrators. Instead of manually enabling the audit tracking, we will discuss the process of automating it for multiple entities.

Use Cases

Auditing in Dynamics CRM is a helpful tool for organizations. It's used for different things.

1. Following Rules and Staying Secure

  • Keep a record of changes to follow industry rules and keep things secure.
  • Track changes to important data for safety and accountability.

2. Understanding Data and Making Reports

  • Look at how data changed over time to learn things for the business.
  • Make reports using audit data to see how people use the system.

3. Fixing Problems and Finding Mistakes

  • Find where mistakes in data come from and fix them.
  • Solve problems by looking at the history of changes in specific records.

Step-by-Step Process to Enable Audit Tracking Using PowerShell

Step 1. Install the Microsoft.Xrm.Data.Powershell Module.

Install-Module Microsoft.Xrm.Data.Powershell -Scope CurrentUser

Step 2. Establish CRM Connection

Connect to your Dynamics CRM instance using PowerShell. Replace the placeholder values with your CRM instance details.

$conn = Get-CrmConnection -ConnectionString "AuthType=ClientSecret;Url=yourCRM_Instance_URL;ClientId=yourClientId;ClientSecret=yourClientSecret"

Step 3. Create the Enable-AuditTracking Function.

function Enable-AuditTracking($EntityName) {
    $entityRequest = New-Object Microsoft.Xrm.Sdk.Messages.RetrieveEntityRequest  
    $entityRequest.LogicalName = $EntityName 
    $entityRequest.RetrieveAsIfPublished = $true 
    $entityResponse = $conn.ExecuteCrmOrganizationRequest($entityRequest) 
    $isAuditEnabled = $entityResponse.EntityMetadata.IsAuditEnabled 

    # Verify if audit is not already enabled 
    if ($isAuditEnabled.Value -eq $false) { 
        Write-Host "Audit tracking is not enabled for $EntityName"
        Write-Host "Enabling Audit for $EntityName"
        $entityResponse.EntityMetadata.IsAuditEnabled.Value = $true 
        $entityUpdate = New-Object Microsoft.Xrm.Sdk.Messages.UpdateEntityRequest 
        $entityUpdate.Entity = $responseEntity.EntityMetadata 
        $updateResponse = $conn.ExecuteCrmOrganizationRequest($entityUpdate) 
        Write-Host "Audit enabled for $EntityName"
    } else { 
        Write-Host "Audit is already enabled for entity $EntityName"

Step 4. Enable Audit Tracking for Multiple Entities. Specify the entities for which you want to enable audit tracking. The script iterates through the entities, calling the previously defined function.

$entitiesToEnableAuditTracking = "contact", "sharepointsite", "sample_product"

foreach ($entity in $entitiesToEnableAuditTracking) {
    Enable-AuditTracking -EntityName $entity

Sample Output

Sample output

Step 5. Publish Metadata Changes

After enabling auditing, it's essential to publish metadata changes to make the configurations take effect.

$Publish = New-Object Microsoft.Crm.Sdk.Messages.PublishAllXmlRequest


PowerShell is a strong and easy way to turn on audit tracking in Dynamics CRM. If administrators follow the steps in this guide, they can make sure their CRM keeps a good record of changes. This helps follow rules, make things more secure, and understand how data is used. Using PowerShell for audit tracking is an important skill for Dynamics CRM administrators, whether they're doing it for rules or to understand what users are doing. We can implement this script as part of our Pre-Post deployment steps during deployment automation as well. Please feel free to reach out in case of any concerns.

Similar Articles