Pipe Gmail Data to CSV in PowerShell

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Gmail Cmdlets

An easy-to-use set of PowerShell Cmdlets offering real-time access to Gmail data. The Cmdlets allow users to easily read, write, update, and delete live data - just like working with SQL server.

Use standard PowerShell cmdlets to access Gmail tables.

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

Creating a Connection to Your Gmail Data

There are two ways to authenticate to Gmail. Before selecting one, first ensure that you have enabled IMAP access in your Gmail account settings. See the "Connecting to Gmail" section under "Getting Started" in the installed documentation for a guide.

The User and Password properties, under the Authentication section, can be set to valid Gmail user credentials.

Alternatively, instead of providing the Password, you can use the OAuth authentication standard. To access Google APIs on behalf on individual users, you can use the embedded credentials or you can register your own OAuth app.

OAuth also enables you to use a service account to connect on behalf of users in a Google Apps domain. To authenticate with a service account, you will need to register an application to obtain the OAuth JWT values.

In addition to the OAuth values, you will need to provide the User. See the "Getting Started" chapter in the help documentation for a guide to using OAuth.

$conn = Connect-Gmail  -User "$User" -Password "$Password"

Selecting Data

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

Select-Gmail -Connection $conn -Table Inbox | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myInboxData.csv -NoTypeInformation

You will notice that we piped the results from Select-Gmail 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-Gmail -Connection $conn -Table Inbox -Where "From = test@test.com" | Remove-Gmail

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

Import-Csv -Path C:\MyInboxUpdates.csv | %{
  $record = Select-Gmail -Connection $Gmail -Table Inbox -Where ("Id = `'"+$_.Id+"`'")
    Update-Gmail -Connection $gmail -Table Inbox -Columns ("Subject","Size") -Values ($_.Subject, $_.Size) -Where ("Id = `'"+$_.Id+"`'")
    Add-Gmail -Connection $gmail -Table Inbox -Columns ("Subject","Size") -Values ($_.Subject, $_.Size)

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!