To make your custom cmdlets (functions) available in your PowerShell sessions, there are a few methods.
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.
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.
« ‹ | November 2024 | › » | ||||
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |