Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Tuesday, April 19, 2011

Creating a PowerShell Module

In Monday’s post, I showed you how to use Dot Sourcing to utilize the functions in one script, in the shell or in another script.  Today we are going to look at how to create a module.  First, we need to create a few files in your local profile.  We are going to call this our Library module.  Create the following folders inside your My Documents folder.


WindowsPowerShell/Modules/Library

Now save this function to a script called Library.psm1.


Function Display-Info
{
 Write-Host (Get-WmiObject Win32_LocalTime).Year
}

Copy Libray.psm1 into the folder you just created.

Remember, the name of the .psm1 file needs to match the directory name.

Now, open a PowerShell session.

Type Get-Module –ListAvailable.  You should see your module listed.

Type Import-Module Library and press enter.

Now execute the function Display-Info.  The tab completion functionality of PowerShell should be available to you.

You can remove this module from memory by typing Remove-Module

No comments: