Ready to get started?

Learn more about the CData ADO.NET Provider for Dynamics CRM or download a free trial:

Download Now

Automate Dynamics CRM Integration Tasks from PowerShell

Are you looking for a quick and easy way to access Dynamics CRM data from PowerShell? We show how to use the Cmdlets for Dynamics CRM and the CData ADO.NET Provider for Dynamics CRM to connect to Dynamics CRM data and synchronize, automate, download, and more.

The CData Cmdlets for Dynamics CRM are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Dynamics CRM.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Dynamics CRM API, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Dynamics CRM data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Dynamics CRM. To access Dynamics CRM data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Dynamics CRM.

After obtaining the needed connection properties, accessing Dynamics CRM data in PowerShell consists of three basic steps.

The connection string options meet the authentication and connection requirements of different Dynamics CRM instances. To connect to your instance, set the User and Password properties, under the Authentication section, to valid Dynamics CRM user credentials and set the Url to a valid Dynamics CRM server organization root. Additionally, set the CRMVersion property to 'CRM2011+' or 'CRMOnline'. IFD configurations are supported as well; set InternetFacingDeployment to true.

Additionally, you can provide the security token service (STS) or AD FS endpoint in the STSURL property. This value can be retrieved with the GetSTSUrl stored procedure. Office 365 users can connect to the default STS URL by simply setting CRMVersion.

PowerShell

  1. Install the module:

    Install-Module DynamicsCRMCmdlets
  2. Connect:

    $dynamicscrm = Connect-DynamicsCRM -User "$User" -Password "$Password" -URL "$URL" -CRM Version "$CRM Version"
  3. Search for and retrieve data:

    $firstname = "Bob" $account = Select-DynamicsCRM -Connection $dynamicscrm -Table "Account" -Where "FirstName = `'$FirstName`'" $account

    You can also use the Invoke-DynamicsCRM cmdlet to execute SQL commands:

    $account = Invoke-DynamicsCRM -Connection $dynamicscrm -Query 'SELECT * FROM Account WHERE FirstName = @FirstName' -Params @{'@FirstName'='Bob'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Dynamics CRM\lib\System.Data.CData.DynamicsCRM.dll")
  2. Connect to Dynamics CRM:

    $conn= New-Object System.Data.CData.DynamicsCRM.DynamicsCRMConnection("User=myuseraccount;Password=mypassword;URL=https://myOrg.crm.dynamics.com/;CRM Version=CRM Online;") $conn.Open()
  3. Instantiate the DynamicsCRMDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT FirstName, NumberOfEmployees from Account" $da= New-Object System.Data.CData.DynamicsCRM.DynamicsCRMDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.firstname $_.numberofemployees }

Update Dynamics CRM Data

PowerShell

Update-DynamicsCRM -Connection $DynamicsCRM -Columns @('FirstName','NumberOfEmployees') -Values @('MyFirstName', 'MyNumberOfEmployees') -Table Account -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsCRM.DynamicsCRMCommand("UPDATE Account SET FirstName='Bob' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.DynamicsCRM.DynamicsCRMParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Dynamics CRM Data

PowerShell

Add-DynamicsCRM -Connection $DynamicsCRM -Table Account -Columns @("FirstName", "NumberOfEmployees") -Values @("MyFirstName", "MyNumberOfEmployees")

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsCRM.DynamicsCRMCommand("INSERT INTO Account (FirstName) VALUES (@myFirstName)", $conn) $cmd.Parameters.Add(new System.Data.CData.DynamicsCRM.DynamicsCRMParameter("@myFirstName","Bob")) $cmd.ExecuteNonQuery()

Delete Dynamics CRM Data

PowerShell

Remove-DynamicsCRM -Connection $DynamicsCRM -Table "Account" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsCRM.DynamicsCRMCommand("DELETE FROM Account WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.DynamicsCRM.DynamicsCRMParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()