Load Dynamics NAV CmdLets in PowerShell ISE with Profiles

I think we can all agree that we like the Windows PowerShell ISE, right? You know, the “C/SIDE for PowerShell” ;-), the editor, this one:


And the one thing that is somewhat less convenient, is the fact that you always have to load the NAV CmdLets into your runspace by typing these lines:

Import-Module 'C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1' -WarningAction SilentlyContinue | out-null
Import-Module 'C:\Program Files\Microsoft Dynamics NAV\80\Service\NavAdminTool.ps1' -WarningAction SilentlyContinue | Out-Null

During the NAVTechDays Pre-Conference workshop, someone (forgot his name – sorry :() pointed out to me that it was possible to add this in the “Profile”, so that it gets loaded every time you start the ISE.

Huh, profile?

That was my first reaction. :-). Now, it’s actually quite simple. All you need to do is create a file following these guidelines: How to Use Profiles in Windows PowerShell ISE

So .. we would like all the users to benefit from our new addition in the profile, so Start the ISE as an Administrator, and execute this:

if (!(test-path $profile.AllUsersCurrentHost))
{new-item -type file -path $profile.AllUsersCurrentHost -force}
psEdit $profile.AllUsersCurrentHost

It will create the profile-file if it didn’t exist yet, and open it in the ISE editor, ready for you to add some code. Now, before we start adding code .. let’s first see what I would like to create:


An extra menu-item the you can click to load the NAV2015 CmdLets (and accordingly, you can add menu-items for multiple versions if you like). Well, your profile-file is open. Add this code in it:

$code =
{
 Import-Module 'C:\Program Files (x86)\Microsoft Dynamics NAV\80\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1' -WarningAction SilentlyContinue | out-null
 Import-Module 'C:\Program Files\Microsoft Dynamics NAV\80\Service\NavAdminTool.ps1' -WarningAction SilentlyContinue | Out-Null
}
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Load NAV 2015 CmdLets',$code,$null)

 

You see it’s quite simple: we put a codeblock with code into a variable, and we add a menu-item in the ISE, that executes this codeblock. As simple as that!

Now, because your profile will always be loaded, it will create a menuitem with exactly that code every time you open the ISE. Just click, and your favorite CmdLets will be loaded into the runspace!

Enjoy!

Ps: I actually just showed you an easy way to make available your scripts. I have added a lot more in this profile, loading my “changed” (read: fixed) NAV Commandlets and such, so they are always available in my environment.

5.00 avg. rating (99% score) - 7 votes

Permanent link to this article: https://www.waldo.be/2015/07/20/load-dynamics-nav-cmdlets-in-powershell-ise-with-profiles/

3 comments

3 pings

  1. http://www.softwareanswers.co.uk/software_answers/2014/12/05/ it was this guy!

      • waldo on July 20, 2015 at 8:11 am
        Author

      Yup, now I remember :-). Thanks! (Shame on me … he even sent me this..)

  2. Just as small note, if you want to remove an older or wrong entry try this one:

    $MyEntry = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus | ?{$_.DisplayName -eq “Load NAV 2015 CmdLets”}
    $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Remove($MyEntry)

  1. […] Bron : Waldo’s Blog Lees meer… […]

  2. […] Via Eric Wauters […]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.