All About Modules

All About Modules

  • Comments 4

One of the coolest new features of Windows PowerShell is PowerShell modules.  I've just written a tutorial about how to use them on my personal blog.

You can check out the full post for more details, but here are some easy things to remember about modules:

  • Modules can be scripts with functions, scripts that include other functions, or assemblies (DLLs) containing Cmdlets or providers
  • Modules let you cleanly separate code
  • Modules make it easy to depend on other code
  • Modules can contain private variables or functions
  • Modules let you refer to functions more explicitly (with & (Get-Module ModuleName) FunctionName )
  • You can take any .PS1 file that contains functions, and save it as a .PSM1, and you have made a module

The module examples also include a few PowerShell functions you might use in day-to-day life, such as being able to Search-WindowsDesktop, Get-RecordedTV, or Remove-RecordedTVBySeries, and are a great practical example of how you can use modules to cleanly separate code.   Check it out.

Hope this helps,

James Brundage [MSFT]

Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
  • I just keep getting "The term 'Add-Module' is not recognized as a cmdlet..."

    And yes, I do have CTP 2.0

    Any ideas?

  • There's no Add-Module, atleast in CTP3...

    PS> Get-Help *-module

    New-Module

    Import-Module

    Get-Module

    Remove-Module

  • I get this:

    PS C:\> Import-Module

    The term 'Import-Module' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and

    try again.

    At line:1 char:14

    + Import-Module <<<<

    and PS> Get-Help *-module returns nothing...

  • Are there any hooks that I can use when loading/unloading a module?

    For example - I would like to have a module that helps with communicating to MSSQL.  It would be efficient if I could open the database connection once at load time, keep the same connection open for multiple DB queries and close/dispose of the connection nicely when I'm finished with it.  It would also be good if it handled errors being thrown elsewhere in the script.

Page 1 of 1 (4 items)