Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate Access Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Access data from PowerShell? This article demonstrates how to utilize the Access Cmdlets for tasks like connecting to Access data, automating operations, downloading data, 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 and bidirectional access to Access.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Access, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete 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.
Once you have acquired the necessary connection properties, accessing Access data in PowerShell can be enabled in three steps.
To connect, set the DataSource property to the path to the Access database.
PowerShell
-
Install the module:
Install-Module AccessCmdlets
-
Connect:
$access = Connect-Access -DataSource "$DataSource"
-
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
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Access\lib\System.Data.CData.Access.dll")
-
Connect to Access:
$conn= New-Object System.Data.CData.Access.AccessConnection("DataSource=C:/MyDB.accdb;") $conn.Open()
-
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 }
Update Access Data
PowerShell
Update-Access -Connection $Access -Columns @('OrderName','Freight') -Values @('MyOrderName', 'MyFreight') -Table Orders -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Access.AccessCommand("UPDATE Orders SET ShipCity='New York' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Access.AccessParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Access Data
PowerShell
Add-Access -Connection $Access -Table Orders -Columns @("OrderName", "Freight") -Values @("MyOrderName", "MyFreight")
ADO.NET
$cmd = New-Object System.Data.CData.Access.AccessCommand("INSERT INTO Orders (ShipCity) VALUES (@myShipCity)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Access.AccessParameter("@myShipCity","New York")))
$cmd.ExecuteNonQuery()
Delete Access Data
PowerShell
Remove-Access -Connection $Access -Table "Orders" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Access.AccessCommand("DELETE FROM Orders WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Access.AccessParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject