PowerShell and Dynamics NAV: Loading the right management scripts

When using PowerShell to administer Dynamics NAV or for development purposes such as upgrades, it is always important to use the right version of the PowerShell scripts that are supplied with Dynamics NAV.

This is because there may be new functionality introduced in one version of Dynamics NAV through a cumulative update or because you are going to perform actions that interact with Dynamics NAV services.

Normally you would manually import the scripts into PowerShell which may take valuable time and will have to be done again if you close PowerShell for any reason. Also when importing the cmdlets provided in the service folder it will check the registry and load the cmdlets for the last cumulative update of Dynamics NAV that you installed which may be a problem as it can start complaining about the version of the Dynamics NAV service that you are working on, which may be on a different cumulative update.

To this end I have created a PowerShell script that aims to solve these issues and load the right script with just a few clicks!

This script will show the window below, where the current Dynamics NAV versions are listed.

Powershell 1

Clicking one of these buttons will build a list of client folders/cumulative updates that are installed for that version of Dynamics NAV and show that list as a new window, as can be seen below.

Powershell 2

Now what happens depends on if PowerShell was started as an administrator.

If it wasn’t started as an administrator it will load the cmdlets in the client folder that was selected and then will load the cmdlets from the service folder for the cumulative update that was last installed.

If it was started as an administrator it will check the version number for the selected client folder and try to find a service folder with the same version and if it succeeds it will load the cmdlets from this folder. The reason this only happens when PowerShell runs as an administrator is that my script will need access to the registry to make sure the cmdlets from the correct service folder are loaded.

At the end of the script it will show you which cmdlets were loaded, from where and which versions.

Powershell 3

Leave a Reply