Microsoft Dynamics 365 Blog

Anyone who uses Windows PowerShell regularly will be familiar with re-typing the names of Windows PowerShell cmdlets they already used many times before. In this post we will show a couple of examples of creating functions and store them in your profile to reduce the amount of re-typing needed in order to make your life easier.

Customer Story

Ever find that you reuse the same PowerShell cmdlets again and again? Import-Module, then Get-NAVServerInstance, Get-NAVServerConfiguration, and repeat typing servername like DynamicsNAV90 as parameter. This post shows how you can create functions for this, put them into your profile and then not have to re-type these commands. You can even put the Dynamics NAV Server name into the profile, and get for example DatabaseName in just two steps (only one line of scriping):

  1. Open Windows PowerShell.
  2. Enter Get-NAVConf DatabaseName.

Example – Get NAV Server Configuration Value

The very common Windows PowerShell script to read your Dynamics NAV Server configuration values:

import-module 'C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll'
Get-NAVServerConfiguration DynamicsNAV90

This returns a list of all configuration parameters, not just the one you were looking for. Here is a way to look up a specific parameter, for example DatabaseName:

$Conf = Get-NAVServerConfiguration DynamicsNAV90
($Conf | where { $_.GetAttribute("key") -eq "DatabaseName" }).Value

But this is two lines and a bit more complex than just running Get-NAVSercerConfiguration. To make it easier to run, we can bake it into a function: Just surround the lines with Function Get-NAVConf{ “ and of course an ending }. Adding a parameter and a bit extra, it could look like this:

$NAVServerInstanceName = "DynamicsNAV90"
$VersionNo = 90
Function Get-NAVConf{
Import-Module "C:\Program Files\Microsoft Dynamics NAV\$VersionNo\Service\Microsoft.Dynamics.Nav.Management.dll"
Write-Host "$KeyName ="
if (!$KeyName){ Get-NAVServerConfiguration $NAVServerInstanceName}
$Conf = Get-NAVServerConfiguration $NAVServerInstanceName
($Conf | where { $_.GetAttribute("key") -eq $KeyName }).Value

After running this once, we have the new function Get-NAVConf available to call like this:

Get-NAVConf DatabaseName

Or call it without a parameter to get a list of configuration values.

Profile it

Run this to see your profile:

Notepad $profile

Whatever script you put into this file will run when you start your Windows PowerShell environment. So copy your function into this file, and you will have it available every time you start PowerShell.

The Dynamics NAV Management module is version-specific, so you must import the relevant version, such as Dynamics NAV 2016:

Import-Module "C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll"

This sets the scope for the Windows PowerShell session to Dynamics NAV 2016, so if you sometimes use Windows PowerShell for different versions of Dynamics NAV, you might want to consider having a profile for each, so that you can load the management module for the version of Dynamics NAV that you will be working on.

Or enclose script in your profile file with <# … script … #> to comment it, then it won’t actually run but is always ready at hand to copy and paste into Windows PowerShell by just opening your profile file:

Notepad $profile

What else

Just as an example, another useful function to have in your profile file could be to look up the startup parameters for your Dynamics NAV Server service in the application log. Something that is a pretty common task which will otherwise take you into the Event Viewer. Doing it from Windows PowerShell means one less reason to leave your PowerShell environment:

$NAVServerInstanceName = "DynamicsNAV90"
$NAVEventProviderName = "MicrosoftDynamicsNavServer`$$NAVServerInstanceName"

Function Get-NAVStartup

If (!$pattern){$pattern = "/$NAVServerInstanceName/"}
Write-Host "Pattern = $pattern"
$Events = Get-WinEvent -ProviderName $NAVEventProviderName -MaxEvents 100
$Events | Select-String -inputobject {$_.message} -pattern $Pattern

Run it without parameters to get the startup parameters, or set the pattern parameter to look up something specific.

What do you do?

Have any really useful functions? Helpful $profile tips? Feel free to share in comment below.


Best regards,

Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV


We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!