Automate HubSpot Integration Tasks from PowerShell

Ready to get started?

Download a free trial:

Download Now

Learn more:

HubSpot ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with HubSpot marketing automation platform including Contacts, Deals, Emails, Companies, and more!



Are you looking for a quick and easy way to access HubSpot data from PowerShell? We show how to use the Cmdlets for HubSpot and the CData ADO.NET Provider for HubSpot to connect to HubSpot data and synchronize, automate, download, and more.

The CData Cmdlets for HubSpot 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 HubSpot.

Cmdlets or ADO.NET?

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

After obtaining the needed connection properties, accessing HubSpot data in PowerShell consists of three basic steps.

HubSpot uses the OAuth authentication standard. You can use the embedded OAuthClientId, OAuthClientSecret, and CallbackURL or you can obtain your own by registering an app.

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

PowerShell

  1. Install the module:

    Install-Module HubSpotCmdlets
  2. Connect:

    $hubspot = Connect-HubSpot
  3. Search for and retrieve data:

    $region = "ONTARIO" $prospects = Select-HubSpot -Connection $hubspot -Table "Prospects" -Where "Region = `'$Region`'" $prospects

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

    $prospects = Invoke-HubSpot -Connection $hubspot -Query 'SELECT * FROM Prospects WHERE Region = @Region' -Params @{'@Region'='ONTARIO'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.HubSpot.HubSpotConnection("InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the HubSpotDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Slug, PageViews from Prospects" $da= New-Object System.Data.CData.HubSpot.HubSpotDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.slug $_.pageviews }

Update HubSpot Data

PowerShell

Update-HubSpot -Connection $HubSpot -Columns @('Slug','PageViews') -Values @('MySlug', 'MyPageViews') -Table Prospects -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.HubSpot.HubSpotCommand("UPDATE Prospects SET Region='ONTARIO' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.HubSpot.HubSpotParameter("@myId","skycomp-solutions-inc")) $cmd.ExecuteNonQuery()

Insert HubSpot Data

PowerShell

Add-HubSpot -Connection $HubSpot -Table Prospects -Columns @("Slug", "PageViews") -Values @("MySlug", "MyPageViews")

ADO.NET

$cmd = New-Object System.Data.CData.HubSpot.HubSpotCommand("INSERT INTO Prospects (Region) VALUES (@myRegion)", $conn) $cmd.Parameters.Add(new System.Data.CData.HubSpot.HubSpotParameter("@myRegion","ONTARIO")) $cmd.ExecuteNonQuery()

Delete HubSpot Data

PowerShell

Remove-HubSpot -Connection $HubSpot -Table "Prospects" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.HubSpot.HubSpotCommand("DELETE FROM Prospects WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.HubSpot.HubSpotParameter("@myId","skycomp-solutions-inc")) $cmd.ExecuteNonQuery()