Load modules by default when opening PowerShell

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.

Important: When using custom profiles on your SharePoint servers in combination the the Userprofile Synchronization Service and use the profile.ps1 approach you could be getting in a situation that your Forefront Identity Manager Service and ForeFron Identity Manager Synchronization Service do not start any more after a reboot. This is because of the profile.ps1 file. Remove the profile and it will start again.

Related Posts

SharePoint Rest API Handler SharePoint contains a lot of Rest API’s that can be used for many scenario’s. You could use them for example in desktop and windows phone applications...
Adding Azure App Service Application Settings with PowerShell Within Azure there is a option to change several configuration settings. When working with  Deployment Slots this means you have to click a lot within...
Creating the Release Template that runs after the Build – Part 3 With the component created in part 2 we can start using it to deploy applications to our SharePoint environment. We start by creating a new template. ...
Create a custom component to deploy SharePoint Solutions – Part 2 In the first part of the series we have adjusted a build template in order to copy content from source control to the drop location after the build. T...
Offline Installation SharePoint 2013 In many situation you would like to do a offline installation of SharePoint 2013 or you are required to do a offline installation because you do not h...
Configuring Web Site Binding with Release Management Release Management provides a continuous deployment solution that makes release cycles repeatable, visible, and more efficient by automating deploymen...

Leave a Reply

Your email address will not be published. Required fields are marked *