PowerShell is a versatile scripting environment that allows users to extend its capabilities by creating custom functions. Once written, you can load these PowerShell custom functions into your session or environment for reuse. Here are four common methods to load custom functions in PowerShell.
Pasting Custom Functions Directly into the PowerShell Terminal
Pasting a function directly into the terminal is the simplest way to temporarily use it. Once loaded, the function is available until the session is closed. This method is good for testing as you are developing the function code.
- Copy the function definition.
- Paste it directly into your PowerShell terminal.
- Hit
Enter
to load it into the current session.
Example:
function Get-Greeting {
param ([string]$Name)
"Hello, $Name!"
}
# After pasting, you can call:
Get-Greeting -Name "Alice"
Adding Custom Functions to Your PowerShell Profile
For functions you use frequently, add them to your PowerShell profile. The profile script runs automatically when a new session starts. You can view the path to your PowerShell profile by entering $PROFILE
into your PowerShell terminal.
Read More: about_Profiles documentation
- Open your profile:
notepad $PROFILE
- Add your function definition to the file.
- Save and close the file.
- Reload your profile to use the function immediately:
.$PROFILE
Note: Ensure you have write access to the profile file. Create a profile file using New-Item -ItemType File -Path $PROFILE
if it doesn’t exist.
Dot-Sourcing a .ps1 File
You can use dot-sourcing if you have multiple functions or prefer to organize your code in files. Dot-sourcing is also temporary; you would need to load the function files each time you want to use them.
- Save the function in a
.ps1
file (e.g.,MyFunctions.ps1
). - Load it into your session:
.\MyFunctions.ps1
Key Points:
- Ensure the file is in a location accessible from your terminal.
- You’ll need to perform this action each time to load the script into the current scope.
Creating a Module Using a .psm1 File
Modules are the most scalable way to manage and share PowerShell functions. You can view where PowerShell loads modules from by viewing the value of the environment variable $env:PSModulePath
. Saving a custom module to one of these paths ensures the functions are available each time you open a PowerShell terminal window.
- Save your function in a
.psm1
file (e.g.,MyModule.psm1
). - Create a module folder with the same name as the
.psm1
file:MyModule\MyModule.psm1
- Place the folder in one of the PowerShell module paths (e.g.,
C:\Users\<User>\OneDrive\Documents\PowerShell\Modules\
). - Import the module:
Import-Module MyModule
.
Managing Modules:
- Check available modules:
Get-Module -ListAvailable
- Remove a loaded module:
Remove-Module MyModule
Loading PowerShell Custom Functions Summary
Choosing the suitable method to load a custom function depends on your needs:
- Temporary: Paste directly into the terminal.
- Persistent: Add to the profile.
- Reusable and Organized: Dot-source
.ps1
files. - Scalable and Shareable: Create modules with
.psm1
files.
Which method do you use most often? Let us know in the comments below!
Read More: Check out more great PowerShell content here!