Ready to get started?

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

 Download Now

Learn more:

Dynamics NAV Icon Dynamics NAV ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Dynamics NAV data including Items, Sales Orders, Purchase Orders, and more!

Automate Dynamics NAV Integration Tasks from PowerShell



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

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

PowerShell Cmdlets or ADO.NET Provider?

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

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

Before you can connect, OData Services will need to be enabled on the server. Once OData Services are enabled, you will be able to query any Services that are published on the server.

The User and Password properties, under the Authentication section, must be set to valid Dynamics NAV user credentials. In addition, you will need to specify a URL to a valid Dynamics NAV server organization root and a ServerInstance. If there is not a Service Default Company for the server, you will need to set the Company as well.

PowerShell

  1. Install the module:

    Install-Module DynamicsNAVCmdlets
  2. Connect:

    $dynamicsnav = Connect-DynamicsNAV -http://myserver:7048 "$http://myserver:7048" -User "$User" -Password "$Password" -ServerInstance "$ServerInstance"
  3. Search for and retrieve data:

    $name = "Bob" $customer = Select-DynamicsNAV -Connection $dynamicsnav -Table "Customer" -Where "Name = `'$Name`'" $customer

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

    $customer = Invoke-DynamicsNAV -Connection $dynamicsnav -Query 'SELECT * FROM Customer WHERE Name = @Name' -Params @{'@Name'='Bob'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.DynamicsNAV.DynamicsNAVConnection("http://myserver:7048;User=myserver\Administrator;Password=admin;ServerInstance=DYNAMICSNAV71;") $conn.Open()
  3. Instantiate the DynamicsNAVDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, Prices_Including_VAT from Customer" $da= New-Object System.Data.CData.DynamicsNAV.DynamicsNAVDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.prices_including_vat }

Update Dynamics NAV Data

PowerShell

Update-DynamicsNAV -Connection $DynamicsNAV -Columns @('Name','Prices_Including_VAT') -Values @('MyName', 'MyPrices_Including_VAT') -Table Customer -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsNAV.DynamicsNAVCommand("UPDATE Customer SET Name='Bob' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.DynamicsNAV.DynamicsNAVParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Dynamics NAV Data

PowerShell

Add-DynamicsNAV -Connection $DynamicsNAV -Table Customer -Columns @("Name", "Prices_Including_VAT") -Values @("MyName", "MyPrices_Including_VAT")

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsNAV.DynamicsNAVCommand("INSERT INTO Customer (Name) VALUES (@myName)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.DynamicsNAV.DynamicsNAVParameter("@myName","Bob"))) $cmd.ExecuteNonQuery()

Delete Dynamics NAV Data

PowerShell

Remove-DynamicsNAV -Connection $DynamicsNAV -Table "Customer" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.DynamicsNAV.DynamicsNAVCommand("DELETE FROM Customer WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.DynamicsNAV.DynamicsNAVParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()