Publish duplicate detection rule using PowerShell


In D365 project PowerShell scripts are used for a number of tasks. A few examples are:

  1. To automate import and export of solution
  2. For automating manual deployment tasks or steps
  3. Creating reference data
  4. Querying, testing and analysing data while in development, and also post-deployment
  5. Creating integration tests to ensure the system is in expected state

Recently I used PowerShell script to ensure duplicate detection rules are always published after release.

About Script

This script uses “Microsoft.Xrm.Data.Powershell” module. Duplicate detection rule name is passed as an argument, and this script will publish it if it was not.

function Publish-CrmDuplicateDetectionRules{

[parameter(Mandatory=$true, Position=1)]
[parameter(Mandatory=$false, Position=2)]

$fetch = @”
<entity name=”duplicaterule” >
<condition attribute=”statuscode” operator=”eq” value=”0″ />
<condition attribute=”name” operator=”eq” value=”$DuplicateDetectionRule”/>
$matchingDDRules = Get-CrmRecordsByFetch -conn $conn -Fetch $fetch

Write-Host $matchingDDRules.Count “rules found”

if($matchingDDRules.Count -lt 1)
throw “Duplicate rule $DuplicateDetectionRule did not exist”


if($PublishAll -eq $false)
{ Write-Host “Publishing one rule”
$ddRule_toPublish = New-Object Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleRequest
$ddRule_toPublish.DuplicateRuleId= $matchingDDRules.CrmRecords[0].duplicateruleid
{ Write-Host “Publishing rules”
foreach($rule in $matchingDDRules.CrmRecords)
write-host “rule is ” $rule.duplicateruleid
$ddRule_toPublish = New-Object Microsoft.Crm.Sdk.Messages.PublishDuplicateRuleRequest
$ddRule_toPublish.DuplicateRuleId= $rule.duplicateruleid
Write-Host “Rule Published”



Enjoy your 365 day 🙂

About Me 🙂

I m an IT consultant working in Melbourne Australia. I solve business problems using Microsoft technologies (Dynamics 365, Office 365, Azure, Flow, Power Apps, Power BI). I m involved in community activities and I blog at

I love to get connected with people working in IT, providing solutions or who just like Microsoft technologies. To get in touch please follow my blog, and connect through Linkedin, Twitter or Facebook


Twitter: @YawerIqbal

Linkedin: YawerIqbal

Facebook: Yawer.Iqbal


  1. I get this error after I load the function and run the required cmdlet:

    Method invocation failed because [System.String] does not contain a method named ‘ExecuteCrmOrganizationRequest’.
    At line:44 char:9
    + $org.ExecuteCrmOrganizationRequest($ddRule_toPublish,$trace)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

    Would you know what the problem is or what I am missing please?

Leave a Reply to Joanne Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s