Ready to get started?

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

Download Now

Pipe Sugar CRM Data to CSV in PowerShell

Use standard PowerShell cmdlets to access Sugar CRM tables.

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

Creating a Connection to Your Sugar CRM Data

The User and Password properties, under the Authentication section, must be set to valid SugarCRM user credentials. This will use the default OAuth token created to allow client logins. OAuthClientId and OAuthClientSecret are required if you do not wish to use the default OAuth token.

You can generate a new OAuth consumer key and consumer secret in Admin -> OAuth Keys. Set the OAuthClientId to the OAuth consumer key. Set the OAuthClientSecret to the consumer secret.

Additionally, specify the URL to the SugarCRM account.

Note that retrieving SugarCRM metadata can be expensive. It is advised that you store the metadata locally as described in the "Caching Metadata" chapter of the help documentation.

$conn = Connect-SugarCRM  -User "$User" -Password "$Password" -URL "$URL" -CacheMetadata "$CacheMetadata"

Selecting Data

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

Select-SugarCRM -Connection $conn -Table Accounts | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountsData.csv -NoTypeInformation

You will notice that we piped the results from Select-SugarCRM 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-SugarCRM -Connection $conn -Table Accounts -Where "Name = Bob" | Remove-SugarCRM

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 Sugar CRM, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyAccountsUpdates.csv | %{
  $record = Select-SugarCRM -Connection $SugarCRM -Table Accounts -Where ("Id = `'"+$_.Id+"`'")
    Update-SugarCRM -Connection $sugarcrm -Table Accounts -Columns ("Name","AnnualRevenue") -Values ($_.Name, $_.AnnualRevenue) -Where ("Id = `'"+$_.Id+"`'")
    Add-SugarCRM -Connection $sugarcrm -Table Accounts -Columns ("Name","AnnualRevenue") -Values ($_.Name, $_.AnnualRevenue)

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!