Thursday, February 24, 2011

Creating a basic PowerShell script module

It is convenient to put frequently used functions into a PowerShell module. To do this, simply go to (after creating it, if need be) a folder called Documents\WindowsPowerShell\Modules (for the paths you can use, run this command: echo $env:PSModulePath).

Create a folder by the name of the module you want to create e.g. Utilities. Within that folder, create a .psm1 file by the same name as that folder e.g. Utilities.psm1. Any function you create in the .psm1 file will be imported when you import the module.

Additional information:
  • gmo -ListAvailable should list your new module, along with others installed in your system.
  • ipmo Utilities will load your module (if it is called Utilities).
  • rmo Utilities will unload your module (if it is called Utilities).
  • gcm -Module Utilities will list all the functions exported by your module once it is loaded (if it is called Utilities).
  • Export-ModuleMember allows you to choose which functions you export from your module.

0 comments:

Post a Comment