Automate MailChimp Integration Tasks from PowerShell



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 Cmdlets for MailChimp are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to MailChimp.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to MailChimp, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete MailChimp data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for MailChimp. To access MailChimp data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for MailChimp.

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, you will need to 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.

PowerShell

  1. Install the module:

    Install-Module MailChimpCmdlets
  2. Connect:

    $mailchimp = Connect-MailChimp -APIKey "$APIKey"
  3. Search for and retrieve data:

    $contact_country = "US" $lists = Select-MailChimp -Connection $mailchimp -Table "Lists" -Where "Contact_Country = `'$Contact_Country`'" $lists

    You can also use the Invoke-MailChimp cmdlet to execute SQL commands:

    $lists = Invoke-MailChimp -Connection $mailchimp -Query 'SELECT * FROM Lists WHERE Contact_Country = @Contact_Country' -Params @{'@Contact_Country'='US'}

ADO.NET

  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

PowerShell

Update-MailChimp -Connection $MailChimp -Columns @('Name','Stats_AvgSubRate') -Values @('MyName', 'MyStats_AvgSubRate') -Table Lists -Id "MyId"

ADO.NET

$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

PowerShell

Add-MailChimp -Connection $MailChimp -Table Lists -Columns @("Name", "Stats_AvgSubRate") -Values @("MyName", "MyStats_AvgSubRate")

ADO.NET

$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

PowerShell

Remove-MailChimp -Connection $MailChimp -Table "Lists" -Id "MyId"

ADO.NET

$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.