Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate QuickBooks Integration Tasks from PowerShell
Are you in search of a quick and easy way to access QuickBooks data from PowerShell? This article demonstrates how to utilize the QuickBooks Cmdlets for tasks like connecting to QuickBooks data, automating operations, downloading data, and more.
The CData Cmdlets for QuickBooks 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 QuickBooks.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to QuickBooks, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete QuickBooks data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for QuickBooks. To access QuickBooks data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for QuickBooks.
Once you have acquired the necessary connection properties, accessing QuickBooks data in PowerShell can be enabled in three steps.
When you are connecting to a local QuickBooks instance, you do not need to set any connection properties.
Requests are made to QuickBooks through the Remote Connector. The Remote Connector runs on the same machine as QuickBooks and accepts connections through a lightweight, embedded Web server. The server supports SSL/TLS, enabling users to connect securely from remote machines.
The first time you connect, you will need to authorize the Remote Connector with QuickBooks. See the "Getting Started" chapter of the help documentation for a guide.
PowerShell
-
Install the module:
Install-Module QuickBooksCmdlets
-
Connect:
$quickbooks = Connect-QB -URL "$URL" -User "$User" -Password "$Password"
-
Search for and retrieve data:
$type = "Commercial" $customers = Select-QB -Connection $quickbooks -Table "Customers" -Where "Type = `'$Type`'" $customers
You can also use the Invoke-QB cmdlet to execute SQL commands:
$customers = Invoke-QB -Connection $quickbooks -Query 'SELECT * FROM Customers WHERE Type = @Type' -Params @{'@Type'='Commercial'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for QuickBooks\lib\System.Data.CData.QuickBooks.dll")
-
Connect to QuickBooks:
$conn= New-Object System.Data.CData.QuickBooks.QuickBooksConnection("URL=http://remotehost:8166;User=admin;Password=admin123;") $conn.Open()
-
Instantiate the QuickBooksDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, CustomerBalance from Customers" $da= New-Object System.Data.CData.QuickBooks.QuickBooksDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.customerbalance }
Update QuickBooks Data
PowerShell
Update-QB -Connection $QuickBooks -Columns @('Name','CustomerBalance') -Values @('MyName', 'MyCustomerBalance') -Table Customers -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.QuickBooks.QuickBooksCommand("UPDATE Customers SET Type='Commercial' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.QuickBooks.QuickBooksParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert QuickBooks Data
PowerShell
Add-QB -Connection $QuickBooks -Table Customers -Columns @("Name", "CustomerBalance") -Values @("MyName", "MyCustomerBalance")
ADO.NET
$cmd = New-Object System.Data.CData.QuickBooks.QuickBooksCommand("INSERT INTO Customers (Type) VALUES (@myType)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.QuickBooks.QuickBooksParameter("@myType","Commercial")))
$cmd.ExecuteNonQuery()
Delete QuickBooks Data
PowerShell
Remove-QB -Connection $QuickBooks -Table "Customers" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.QuickBooks.QuickBooksCommand("DELETE FROM Customers WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.QuickBooks.QuickBooksParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject