Ready to get started?

Download a free trial of the Twitter Data Provider to get started:

 Download Now

Learn more:

Twitter Icon Twitter ADO.NET Provider

A straightforward interface to connect any .NET application with Twitter integration capabilities including Search, GeoSearch, UserInfo, DirectMessages, Followers, and more!

Automate Twitter Integration Tasks from PowerShell



Are you in search of a quick and easy way to access Twitter data from PowerShell? This article demonstrates how to utilize the Twitter Cmdlets for tasks like connecting to Twitter data, automating operations, downloading data, and more.

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

PowerShell Cmdlets or ADO.NET Provider?

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

Once you have acquired the necessary connection properties, accessing Twitter data in PowerShell can be enabled in three steps.

All tables require authentication. You can connect using your User and Password or OAuth. To authenticate using OAuth, you can use the embedded OAuthClientId, OAuthClientSecret, and CallbackURL or you can register an app to obtain your own.

If you intend to communicate with Twitter only as the currently authenticated user, then you can obtain the OAuthAccessToken and OAuthAccessTokenSecret directly by registering an app.

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

PowerShell

  1. Install the module:

    Install-Module TwitterCmdlets
  2. Connect:

    $twitter = Connect-Twitter
  3. Search for and retrieve data:

    $from_user_name = "twitter" $tweets = Select-Twitter -Connection $twitter -Table "Tweets" -Where "From_User_Name = `'$From_User_Name`'" $tweets

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

    $tweets = Invoke-Twitter -Connection $twitter -Query 'SELECT * FROM Tweets WHERE From_User_Name = @From_User_Name' -Params @{'@From_User_Name'='twitter'}

ADO.NET

  1. Load the provider's assembly:

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

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

    $sql="SELECT From_User_Name, Retweet_Count from Tweets" $da= New-Object System.Data.CData.Twitter.TwitterDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.from_user_name $_.retweet_count }

Update Twitter Data

PowerShell

Update-Twitter -Connection $Twitter -Columns @('From_User_Name','Retweet_Count') -Values @('MyFrom_User_Name', 'MyRetweet_Count') -Table Tweets -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Twitter.TwitterCommand("UPDATE Tweets SET From_User_Name='twitter' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Twitter.TwitterParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Twitter Data

PowerShell

Add-Twitter -Connection $Twitter -Table Tweets -Columns @("From_User_Name", "Retweet_Count") -Values @("MyFrom_User_Name", "MyRetweet_Count")

ADO.NET

$cmd = New-Object System.Data.CData.Twitter.TwitterCommand("INSERT INTO Tweets (From_User_Name) VALUES (@myFrom_User_Name)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Twitter.TwitterParameter("@myFrom_User_Name","twitter"))) $cmd.ExecuteNonQuery()

Delete Twitter Data

PowerShell

Remove-Twitter -Connection $Twitter -Table "Tweets" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Twitter.TwitterCommand("DELETE FROM Tweets WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Twitter.TwitterParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()