Automate MailChimp Integration Tasks from PowerShell

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access MailChimp data from PowerShell? This article demonstrates how to utilize the MailChimp Cmdlets for tasks like connecting to MailChimp data, automating operations, downloading data, and more.

The CData ADO.NET Provider for MailChimp is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MailChimp.

ADO.NET Provider

The ADO.NET Provider provides a SQL interface for MailChimp; this tutorial shows how to use the Provider to create, retrieve, update, and delete MailChimp data.

Once you have acquired the necessary connection properties, accessing MailChimp data in PowerShell can be enabled in three steps.

You can set the APIKey to the key you generate in your account settings, or, instead of providing your APIKey, you can use the OAuth standard to authenticate the application. OAuth can be used to enable other users to access their own data. To authenticate using OAuth, obtain the OAuthClientId, OAuthClientSecret, and CallbackURL by registering an app with MailChimp.

See the "Getting Started" chapter in the help documentation for a guide to using OAuth.

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.MailChimp.MailChimpConnection("APIKey=myAPIKey;")
    $conn.Open()
  3. Instantiate the MailChimpDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, Stats_AvgSubRate from Lists"
    
    $da= New-Object System.Data.CData.MailChimp.MailChimpDataAdapter($sql, $conn)
    $dt= New-Object System.Data.DataTable
    $da.Fill($dt)
    
    $dt.Rows | foreach {
        Write-Host $_.name $_.stats_avgsubrate
    }

Update MailChimp Data

$cmd =  New-Object System.Data.CData.MailChimp.MailChimpCommand("UPDATE Lists SET Contact_Country='US' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MailChimp.MailChimpParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()

Insert MailChimp Data

$cmd =  New-Object System.Data.CData.MailChimp.MailChimpCommand("INSERT INTO Lists (Contact_Country) VALUES (@myContact_Country)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MailChimp.MailChimpParameter("@myContact_Country","US")))
$cmd.ExecuteNonQuery()

Delete MailChimp Data

$cmd =  New-Object System.Data.CData.MailChimp.MailChimpCommand("DELETE FROM Lists WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.MailChimp.MailChimpParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the MailChimp Data Provider to get started:

 Download Now

Learn more:

MailChimp Icon MailChimp ADO.NET Provider

Complete read-write access to MailChimp enables developers to search (Lists, Campaigns, Reports, etc.), update items, edit customers, and more, from any .NET application.