Ready to get started?

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

Download Now

Automate YouTube Analytics Integration Tasks from PowerShell

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

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

Cmdlets or ADO.NET?

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

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

YouTube Analytics uses the OAuth authentication standard. You can use the embedded CData OAuth credentials or you can register an application with Google to obtain your own.

In addition to the OAuth values, to access YouTube Analytics data set ChannelId to the Id of a YouTube channel. You can obtain the channel Id in the advanced account settings for your channel. If not specified, the channel of the currently authenticated user will be used.

If you want to generate content owner reports, specify the ContentOwnerId property. This is the Id of the copyright holder for content in YouTube's rights management system. The content owner is the person or organization that claims videos and sets their monetization policy.

PowerShell

  1. Install the module:

    Install-Module YouTubeAnalyticsCmdlets
  2. Connect:

    $youtubeanalytics = Connect-YouTubeAnalytics -ContentOwnerId "$ContentOwnerId" -ChannelId "$ChannelId"
  3. Search for and retrieve data:

    $mine = "True" $groups = Select-YouTubeAnalytics -Connection $youtubeanalytics -Table "Groups" -Where "Mine = `'$Mine`'" $groups

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

    $groups = Invoke-YouTubeAnalytics -Connection $youtubeanalytics -Query 'SELECT * FROM Groups WHERE Mine = @Mine' -Params @{'@Mine'='True'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsConnection("ContentOwnerId=MyContentOwnerId;ChannelId=MyChannelId;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the YouTubeAnalyticsDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Snippet_Title, ContentDetails_ItemCount from Groups" $da= New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.snippet_title $_.contentdetails_itemcount }

Update YouTube Analytics Data

PowerShell

Update-YouTubeAnalytics -Connection $YouTubeAnalytics -Columns @('Snippet_Title','ContentDetails_ItemCount') -Values @('MySnippet_Title', 'MyContentDetails_ItemCount') -Table Groups -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsCommand("UPDATE Groups SET Mine='True' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert YouTube Analytics Data

PowerShell

Add-YouTubeAnalytics -Connection $YouTubeAnalytics -Table Groups -Columns @("Snippet_Title", "ContentDetails_ItemCount") -Values @("MySnippet_Title", "MyContentDetails_ItemCount")

ADO.NET

$cmd = New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsCommand("INSERT INTO Groups (Mine) VALUES (@myMine)", $conn) $cmd.Parameters.Add(new System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsParameter("@myMine","True")) $cmd.ExecuteNonQuery()

Delete YouTube Analytics Data

PowerShell

Remove-YouTubeAnalytics -Connection $YouTubeAnalytics -Table "Groups" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsCommand("DELETE FROM Groups WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()