Coffee Break – Creating and Using Windows PowerShell Profiles

When you open a Windows PowerShell command prompt, often you type the same commands to begin with. This post shows how to create a profile that will run the commands you want every time you start a new PowerShell command prompt.

Coffee Break 7 – Creating and using Windows PowerShell profiles

User Story

Developer wants to automate importing of  Microsoft Dynamics NAV modules along with other often used scripts into the Windows PowerShell ISE environment, for simplicity and reliability. For this, a PowerShell profile can be used. A PowerShell Profile is a start-up script that runs when PowerShell starts. Here we can import modules , add snap-ins, run functions, etc. Things that will help to initialize and build up the work environment.


A profile path is contained in the automatically created variable $profile. Executing $profile in PowerShell will show the path to your existing profile, or path to where one would be created. It will look like this when run from the ISE environment: C:\Users\[user]\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1.

To check if you already have a profile created, run:

Test-Path $profile

If the profile doesn’t exist, return value will be False. To create the profile you can run this:

New-Item -path $profiletype fileforce
(notice using –force to overwrite existing profile).

The above will simply create a blank profile file (script). One can modify the script directly from the file explorer, and or by using

Notepad $profile


#Here we can for example import modules used  for NAV management.

Import-module “C:\Program Files\Microsoft Dynamics NAV\80\Service\Microsoft.Dynamics.Nav.Management.dll”

#And any other modules we might use frequently (for ex. Azure module – requires having installed this in advance)

Import-module Azure

#Set prompt to be descriptive

function Prompt { “NAV71 $(Get-Location)>” }

# We can set background to our preferences, change default path, etc….

Set-location C:\MyWorkEnv\
$host.UI.RawUI.BackgroundColor = “DarkRed”; $Host.UI.RawUI.ForegroundColor = “Gray

For complete list of colors that can be set using host, check this link: ConsoleColor Enumeration

Note: Some modules get loaded automatically when PowerShell starts, even without Import-Module. This happens when a module is added to system folder %PSModulePath% at installation time. All modules that are in %PSModulePath% are loaded automatically whenever any of their commands are used. 

Different profiles

There are 4 possible profiles for a host:

• All Users, All Hosts,
• All Users, Current Host,
• Current User, All Hosts,
• Current User, Current Host

Where the bottommost one has the highest priority. You can store different profiles for different scopes. To view the path of each, run each of the following commands:

• $Profile.AllUsersAllHosts
• $Profile.AllUsersCurrentHost
• $Profile.CurrentUserAllHosts
• $Profile.CurrentUserCurrentHost


Note also that a Windows PowerShell profile is  just another ps script and like any other ps script is not exempt from execution policy. The Execution Policy in PowerShell determines which scripts can be run and which cannot. By default, no scripts can be run, including Profiles (Restricted Execution Policy). This can be changed (to All Signed, Remote Signed, Unrestricted). Note that the change is saved in the registry and only needs to be changed once per computer.

Handling multiple versions of Microsoft Dynamics NAV on the same machine

Finally, you might want to work with several Dynamics NAV versions in parallel (on the same machine). Working with each version requires it’s respective Dynamics NAV management module version to be loaded. The PS modules are .net assemblies which will load it into the AppDomain of the PowerShell Host (the application). Remove module cmdlet will just remove the module from  the current session. For more details, see:

In short – once we have imported a module (dll) it will remain in the current session. So to work with parallel NAV versions, a session per version is needed. Instead of one common profile, a startup-script dedicated to each session can be used to initialize each NAV version environment.

So to create an ISE environment for each Dynamics NAV version, we can open ISE with a startup script to import Dynamics NAV modules for the relevant version of Microsoft Dynamics NAV. For example, create a script file called MyNAV2013Env.ps1 and save it locally. The file could for example look like following:


import-module “C:\Program Files\Microsoft Dynamics NAV\71\Service\Microsoft.Dynamics.Nav.Management.dll”  
Set-Location C:\tech\MyNav2013Env\

#here we can (for ex.) add different coloring for different version, to avoid any confusion about what NAV version we’re working with
$host.UI.RawUI.BackgroundColor = “DarkRed”; $Host.UI.RawUI.ForegroundColor = “Gray”

#the below will clear the screen

# Welcome message
Write-Host “Welcome to NAV 2013R2 Powershell: “ + $env:Username
Write-Host “Dynamics NAV version 2013 R2 module imported”


Save the file and create a shortcut to the Windows PowerShell ISE on the desktop. Modify shortcut properties to set
Target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe -File “C:\mydocs\ MyNAV2013Env.ps1”
Set shortcut name to NAV2013R2_ISE. Suggestion: Tick “Run as Administrator” on the shortcut as well.

When doubleclicking either of the shortcuts an ISE environment will open with the MyNAV2013Env file.
Press F5 to execute the file and set up environment, then Ctrl+N (or File-New) to open a new script file to work with.

Repeat the above for Microsoft Dynamics NAV 2015 (modify the file name, module path , colors, welcome message and target path accordingly). And in this way have one shortcut for each version of Microsoft Dynamics NAV on your machine.


Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV