Ready to get started?

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

Download Now

Automate Access Integration Tasks from PowerShell

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

The CData Cmdlets for Access are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time access to Access.

Cmdlets or ADO.NET?

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

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

To connect, set the DataSource property to the path to the Access database.

PowerShell

  1. Install the module:

    Install-Module AccessCmdlets
  2. Connect:

    $access = Connect-Access -DataSource "$DataSource"
  3. Search for and retrieve data:

    $shipcity = "New York" $orders = Select-Access -Connection $access -Table "Orders" -Where "ShipCity = `'$ShipCity`'" $orders

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

    $orders = Invoke-Access -Connection $access -Query 'SELECT * FROM Orders WHERE ShipCity = @ShipCity' -Params @{'@ShipCity'='New York'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.Access.AccessConnection("DataSource=C:\MyDB.accdb;") $conn.Open()
  3. Instantiate the AccessDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT OrderName, Freight from Orders" $da= New-Object System.Data.CData.Access.AccessDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.ordername $_.freight }