Auto Load Dynamics NAV Modules On PowerShell Startup

  2 minute read

dialog-148815_1280

Dynamics Navision PowerShell cmdlets were introduced with the release of version 2013 and with every new release they have added more cmdlets to manage Navision. There are several of them which can be used to create instances, to install, backup, upgrade, and automate several tasks. I will discuss several of those cmdlets and some scripts in my next series of blog on PowerShell.

In this post let’s discuss how can we load those cmdlets. When you open PowerShell console or PowerShell ISE, by default it does not load the NAV cmdlets automatically you need to use the following cmdlet to import them Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\71\Service\NavAdminTool.ps1’.

If you develop any other modules, custom functions or scripts you need to execute the same import-module cmdlet for your every .ps1 file path.

How can we automate this so that it will load all custom scripts when you open the Console or ISE without executing the commands ? We can create aliases (which are short name to a cmdlet) but those are only kept for the current session and there are few short-comings which I will discuss in my next post. The best option to load the custom modules,functions or scripts automatically is to use a Profile. A profile is a windows PowerShell script which runs automatically when you start a new session.

There are different type of profiles, but let’s concentrate today on current user profile which is stored in the following directory *%UserProfile%\My Documents\WindowsPowerShell*

To know your profile path you can type $Profile which will return the profile path. To check if the profile file exists or not use the following cmdlet test-path $profile (Which will return true if the file exists or else false)

If the file does not exist, then create a new profile file using the below cmdlet, it will overwrite the existing file if file already exists.new-item –path $profile –itemtype file –force (If you don’t the syntax of any command please use help or Get-Help)

One tip on the Help command, sometimes it is hard to scroll through the help when it displays in the console, you can use the parameter *–ShowWindow to open help in a new GUI window.

For Ex: to know more about Test-Path use Help Test-Path –ShowWindow which will open the help in a new window which is easy to scroll and search as shown below

image

Create a new folder in the C:\ drive and call it UploadScripts. Copy all your custom scripts or functions and in our case we will copy NAVAdminTool.ps1 and will paste in the newly created folder.

Now open the profile file which is created in %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

Add the following code so that everytime the profile is loaded it will load all our scripts.Set-ExecutionPolicy remoteSigned $uploaddir = “C:\UploadScripts” Get-ChildItem “${uploaddir}*.ps1” | %{.$_} Write-Host -fore Green “All Custom Scripts Loaded”

Every time now you open the console or ISE now it will load all the scripts in that folder.

Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.

Leave a comment