Ready to get started?

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

Download Now

Automate WordPress Integration Tasks from PowerShell

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

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

Cmdlets or ADO.NET?

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

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

To connect to WordPress, set the URL property and other authentication properties. WordPress supports Basic (User and Password) and OAuth2.0 authentication, though Basic is recommended for a testing environment only. To connect with OAuth you will need to register an app with WordPress.

See the Getting Started guide in the CData driver documentation for more information.

PowerShell

  1. Install the module:

    Install-Module WordPressCmdlets
  2. Connect:

    $wordpress = Connect-WordPress -Url "$Url"
  3. Search for and retrieve data:

    $id = "1" $categories = Select-WordPress -Connection $wordpress -Table "Categories" -Where "Id = `'$Id`'" $categories

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

    $categories = Invoke-WordPress -Connection $wordpress -Query 'SELECT * FROM Categories WHERE Id = @Id' -Params @{'@Id'='1'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.WordPress.WordPressConnection("Url=http://www.yourwordpresshost.com;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the WordPressDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Id, Name from Categories" $da= New-Object System.Data.CData.WordPress.WordPressDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.name }

Update WordPress Data

PowerShell

Update-WordPress -Connection $WordPress -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table Categories -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("UPDATE Categories SET Id='1' WHERE Id = @myId", $conn) $cmd.Parameters.Add(new System.Data.CData.WordPress.WordPressParameter("@myId","10456255-0015501366")) $cmd.ExecuteNonQuery()

Insert WordPress Data

PowerShell

Add-WordPress -Connection $WordPress -Table Categories -Columns @("Id", "Name") -Values @("MyId", "MyName")

ADO.NET

$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("INSERT INTO Categories (Id) VALUES (@myId)", $conn) $cmd.Parameters.Add(new System.Data.CData.WordPress.WordPressParameter("@myId","1")) $cmd.ExecuteNonQuery()

Delete WordPress Data

PowerShell

Remove-WordPress -Connection $WordPress -Table "Categories" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.WordPress.WordPressCommand("DELETE FROM Categories WHERE Id=@myId", $conn) $cmd.Parameters.Add(new System.Data.CData.WordPress.WordPressParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()