Ready to get started?

Learn more about the CData ADO.NET Provider for Google Contacts or download a free trial:

Download Now

Automate Google Contacts Integration Tasks from PowerShell

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

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

Cmdlets or ADO.NET?

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

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

You can connect to Google APIs on behalf of individual users or on behalf of a domain. Google uses the OAuth authentication standard. See the "Getting Started" section of the help documentation for a guide.

PowerShell

  1. Install the module:

    Install-Module GoogleContactsCmdlets
  2. Connect:

    $googlecontacts = Connect-GoogleContacts
  3. Search for and retrieve data:

    $searchterms = "Durham" $friends = Select-GoogleContacts -Connection $googlecontacts -Table "Friends" -Where "SearchTerms = `'$SearchTerms`'" $friends

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

    $friends = Invoke-GoogleContacts -Connection $googlecontacts -Query 'SELECT * FROM Friends WHERE SearchTerms = @SearchTerms' -Params @{'@SearchTerms'='Durham'}

ADO.NET

  1. Load the provider's assembly:

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

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

    $sql="SELECT Summary, StartDateTime from Friends" $da= New-Object System.Data.CData.GoogleContacts.GoogleContactsDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.summary $_.startdatetime }

Update Google Contacts Data

PowerShell

Update-GoogleContacts -Connection $GoogleContacts -Columns @('Summary','StartDateTime') -Values @('MySummary', 'MyStartDateTime') -Table Friends -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.GoogleContacts.GoogleContactsCommand("UPDATE Friends SET SearchTerms='Durham' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.GoogleContacts.GoogleContactsParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert Google Contacts Data

PowerShell

Add-GoogleContacts -Connection $GoogleContacts -Table Friends -Columns @("Summary", "StartDateTime") -Values @("MySummary", "MyStartDateTime")

ADO.NET

$cmd = New-Object System.Data.CData.GoogleContacts.GoogleContactsCommand("INSERT INTO Friends (SearchTerms) VALUES (@mySearchTerms)", $conn) $cmd.Parameters.Add(new System.Data.CData.GoogleContacts.GoogleContactsParameter("@mySearchTerms","Durham")) $cmd.ExecuteNonQuery()

Delete Google Contacts Data

PowerShell

Remove-GoogleContacts -Connection $GoogleContacts -Table "Friends" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.GoogleContacts.GoogleContactsCommand("DELETE FROM Friends WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.GoogleContacts.GoogleContactsParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()