Did you know that you can manage your Dynamics 365 instance using your favorite automation tool PowerShell? The Microsoft.Xrm.Data.PowerShell-module lets you manage your solutions and such but also has functionality to work on records. In this post I am going to show you how to use the module for data export.
The first thing you need to do is install the module on your system. Just run the command Install-Module Microsoft.Xrm.Data.PowerShell
in PowerShell to get it. Next you need to connect to your instance. There are a few alternatives here so please refer to the project’s GitHub-page. With the Microsoft hosted Dynamics 365 you can use Connect-CrmOnlineDiscovery -InteractiveMode
to connect using a GUI.
Working with data
Once connected you can try querying for some data. The command Get-CrmRecords -EntityLogicalName account
gets you all (or the first 5000) accounts in your system. The output looks something like this:
As you can see the result is an object with a few different properties. The CrmRecords property contains the actual result of the query. So if you want to output all the accounts you would run (Get-CrmRecords -EntityLogicalName account).CrmRecords
. That gets you something like this:
As you can see there doesn’t seem to be any useful data other than the Id to work with. That’s because we haven’t requested any attributes. To include the account name you need to use the -Fields
parameter. Eg. Get-CrmRecords -EntityLogicalName account -Fields "name"
includes the account name in the result like so:
So now that we know how to get data from the system, let’s get it into a CSV-file. You can’t quite do a Get-CrmRecords | Export-CSV
, but close to it. Here’s a code snippet:
The code fetches all accounts from the system, selects just the properties we want and finally exports the result to a CSV-file. You might want to use the -NoTypeInformation
, -Delimeter
and -Encoding
parameters. You can see the final code with some improvements here.
In my next post we will improve on this by supplying our own column names instead of the schema names provided by the system.