Powershell library

KC2TC used Ask the Experts™
I want to create a powershell library and use the functions in my powershell script.Can anybody help me out.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Network Administrator
Sure I do this also.

Create a file like myfunctions.psm1

In the file create all of your functions. Here is an example of one of my functions.

function Unlock-Account{
      Unlock-QADUser -service $gc $username
      Get-AccountStatus $username

Once you are done you load your functions by placing this line in your powershell profile.

import-module \\server\itdep\psscripts\myfunctions.psm1
(Use your path. I keep mine on a share so they are accessable from all servers)

Then all of your functions are available anytime you start powershell.

Good luck!

The way that I use a module for the functionality that I want to be able to use easily is that I have a directory named for my module, and the psm1 file of the same name it in it.  In my case, the directory is:

and the file is called:

In my case the file contains instructions to load the files that make up the functionality that I want:
. $psScriptRoot\GeneralUtilities.ps1
. $psScriptRoot\CompareObjectOutput.ps1
. $psScriptRoot\GetDownloadQuotaLevel.ps1
. $psScriptRoot\GetFileSizesByType.ps1
. $psScriptRoot\ManageBCServices.ps1
. $psScriptRoot\NewPassword.ps1
. $psScriptRoot\SelectItemByAclEntry.ps1

the dot sourcing means load the file and keep the functions in memory.  That way I can keep major functions in their own files for ease of management.

The in my profile I use:
Import-Module PBCPSModule

PowerShell will look in the Module directories under the user hive and under the install directory for a directory called PBCPSModule and in there for the file PBCPSModule.psm1 which it will then execute.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial