Backtrack:  
 
by lunarg on April 19th 2013, at 10:23

To make your custom cmdlets (functions) available in your PowerShell sessions, there are a few methods.

One-time import in the current session

To do a one-time import of functions in your current session, store the functions in a PS1-file and save it somewhere. Then, in your session run the following:

. "path-to-file"

Notice the dot and a space behind it. This method, called "dotting" does a one-time import in the current session. This is useful for when you have functions that you don't use often, and don't want them to be available at all times.

An alternative method is to save the functions as a PSM1-file (extension .psm1) to create a module file, then use Import-Module to import your functions.

Import-Module "path-to-file"

This is the preferred method, and allows for greater control. Check out the resources available on the internet about function libraries for more info.

On a side-note, you can always copy/paste your functions directly in the shell. This will also make them available, but only in the current session. It's very quick-n-dirty, but less convenient to be used a lot.

Permanently import in your profile

Another, perhaps more convenient method to add your custom functions to your sessions, is to use a profile. Powershell profiles are actually just PS1-scripts that get loaded when starting a shell session. The advantage in using profiles is that the functions put in there will be available automatically with each new shell. The downside is that when you have a lot of functions, it takes some time to load them all. Note that profiles only apply to the current user profile.

If you have never used profiles, you probably have to create one. In a Powershell session, run:

New-Item -ItemType File -Path $profile -Force

This creates a new profile, which will be loaded when opening a new shell. To actually add functions to this profile, run this in the shell:

notepad $profile

This will open the profile script of the current shell session. Copy/paste your functions in this file and save it. Then restart the shell to make your functions available. For more information about using profiles, read this topic on MSDN.