Use PS to pull data from CRM Dynamics365

Viewed 302

I've done a bit of research but am having trouble finding any definitive answer on the topic. So I'm here to see if anyone has any experience with getting data out of CRM using PowerShell. I am semi-proficient with Shell so I can make sense of most things. With that said, please don't think I'm here just to be spoon-fed, a nudge in the right direction is what I need.

I am aware there is modules that make this possible (possibly?), and things like XRM toolkit, but a rock-solid answer has been extremely difficult to find.

What I need to do is pull a list of things like "contacts" , "addresses" etc, basically just account details for people stored in the system. It sounds super simple, and doing this is AD, AAD, Exchange is easily done.... but this CRM Dynamics is escaping me.

Thinking of starting off like this:

Import-Module Microsoft.Xrm.Data.Powershell
$conn = Get-CrmConnection -InteractiveMode
$result = Get-CrmRecordsByFetch @"

Then possibly use a Fetch Query? And wrap it up like this:

$result.CrmRecords | Select -Property lastname, firstname, AccountID, Address | Export-Csv -
Encoding UTF8 -Path C:\export.csv -Delimiter ";"

Unfortunately, when it comes to the CRM cmdlets and commands, that's where my knowledge stops.

Any help would be greatly appreciated.

1 Answers

I am not too much familiar with PowerShell but usually you have two options:

If you need to retrieve records using the Microsoft.Xrm.Data.PowerShell you can use Get-CrmRecords or Get-CrmRecordsByFetch, with the second one you can specify a FetchXML query, you can use FetchXML Builder to assist you on creating such query, but I don't use this library

The Invoke-RestMethod is more standard, once you have your OData Query you can execute it but you need to provide a Bearer Token first, in order to generate it you usually need a clientid & clientsecret from an azure app registration.

I have created a tool called Dataverse REST Builder that can assist you creating OData requests, it doesn't provide you with a PowerShell syntax, but you can export your collection to a Thunder Client collection. Once you import it inside Thunder Client you can generate the code for PowerShell.

Related