Load modules by default when opening PowerShell

2 minute read

At the moment we are working a lot with PowerShell in combination with SharePoint. Over time we created a lot of usable functions that were not availible within the default SharePoint Module.

With these functions we created our own PowerShell Module in order to easily load the function within our PowerShell instance. When you would like to know more on how to create a PowerShell Module you can read this TechNet article:

When you have written a module it is relatively easy to load it in the PowerShell instance by using the import-module comment.

import-module [Path To Your Module]\Module.psm1 -WarningAction Ignore

As you can see in the above script we have added the "–WarningAction ignore" because loading a module mostly generate a warning for unapproved Verbs:

“WARNING: The names of some imported commands from the module 'Module' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.”

If you want to stick with the approved Verbs you can take a look at the approved verb list on TechNet:

Now the question still is how do we load it in every PowerShell instance by default. You can accomplish this by creating a “profile.ps1” file. This file needs to be added in the following directory:

C:\Windows\System32\WindowsPowerShell\v1.0\

In the directory “C:\Windows\System32\WindowsPowerShell\v1.0\Example” there is a example profile.ps1 file. This file is empty. For our module we had created the following profile.ps1 file:

#  Copyright (c) Microsoft Corporation.  All rights reserved.
#  
# THIS SAMPLE CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
# WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
# IF THIS CODE AND INFORMATION IS MODIFIED, THE ENTIRE RISK OF USE OR RESULTS IN
# CONNECTION WITH THE USE OF THIS CODE AND INFORMATION REMAINS WITH THE USER.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Write-Host "Loading Microsoft SharePoint PowerShell" -ForegroundColor Green
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

Write-Host "Loading Custom SharePoint PowerShell Module" -ForeGroundColor Green
import-module D:\SCRIPTMODULE\Module.psm1 -WarningAction Ignore

Using the profile.ps1 file make sure that the profile is loaden in every powershell window. You could also use this filename: "Microsoft.PowerShellISE_profile.ps1" to only load within the PowerShell ISE window.