Ready to get started?

Learn more about the Dynamics CRM PowerShell Cmdlets or download a free trial:

Download Now

Pipe Dynamics CRM Data to CSV in PowerShell

Use standard PowerShell cmdlets to access Dynamics CRM tables.

The CData Cmdlets Module for Dynamics CRM is a standard PowerShell module offering straightforward integration with Dynamics CRM. Below, you will find examples of using our DynamicsCRM Cmdlets with native PowerShell cmdlets.

Creating a Connection to Your Dynamics CRM Data

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.

$conn = Connect-DynamicsCRM  -User "$User" -Password "$Password" -URL "$URL" -CRM Version "$CRM Version"

Selecting Data

Follow the steps below to retrieve data from the Account table and pipe the result into to a CSV file:

Select-DynamicsCRM -Connection $conn -Table Account | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountData.csv -NoTypeInformation

You will notice that we piped the results from Select-DynamicsCRM into a Select-Object cmdlet and excluded some properties before piping them into an Export-Csv cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each "row" in the result set, and we do not necessarily want that information in our CSV file.

The Connection, Table, and Columns are appended to the results in order to facilitate piping results from one of the CData Cmdlets directly into another one.

Deleting Data

The following line deletes any records that match the criteria:

Select-DynamicsCRM -Connection $conn -Table Account -Where "FirstName = Bob" | Remove-DynamicsCRM

Inserting and Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Dynamics CRM, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyAccountUpdates.csv | %{
  $record = Select-DynamicsCRM -Connection $DynamicsCRM -Table Account -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-DynamicsCRM -Connection $dynamicscrm -Table Account -Columns ("FirstName","NumberOfEmployees") -Values ($_.FirstName, $_.NumberOfEmployees) -Where ("Id = `'"+$_.Id+"`'")
  }else{
    Add-DynamicsCRM -Connection $dynamicscrm -Table Account -Columns ("FirstName","NumberOfEmployees") -Values ($_.FirstName, $_.NumberOfEmployees)
  }
}

As always, our goal is to simplify the way you connect to data. With cmdlets users can install a data module, set the connection properties, and start building. Download Cmdlets and start working with your data in PowerShell today!