New Module - PowerShell


Overview

New-Module creates a new PowerShell module. Modules are used to organize PowerShell scripts and functions into logical units, making it easier to manage and share code.

Syntax

New-Module [-Name] <string> [-Path] <string> [-Description] <string> [-RequiredAssemblies] <string[]> [-TypesToProcess] <string[]> [-DefaultCommand <string>] [-HelpUri <string>] [-ModuleToProcess <string[]>] [-OutputModuleFile <string>]

Options/Flags

  • -Name: Specifies the name of the module.
  • -Path: Specifies the path to the module directory.
  • -Description: Specifies the description of the module.
  • -RequiredAssemblies: Specifies the assemblies required by the module.
  • -TypesToProcess: Specifies the types to process in the module.
  • -DefaultCommand: Specifies the default command to run when the module is imported.
  • -HelpUri: Specifies the help URI for the module.
  • -ModuleToProcess: Specifies the modules to process in the module.
  • -OutputModuleFile: Specifies the output module file.

Examples

Example 1: Create a module with a default command

New-Module -Name MyModule -Path c:\MyModule -Description "My PowerShell Module" -RequiredAssemblies "System.Management.Automation, System.dll" -DefaultCommand Get-MyStuff

Example 2: Create a module with dependencies

New-Module -Name MyModule -Path c:\MyModule -Description "My PowerShell Module" -RequiredAssemblies "System.Management.Automation, System.dll" -ModuleToProcess "MyOtherModule"

Common Issues

  • Module not loaded: Ensure that the module is located in a directory that is included in the PowerShell path.
  • Module not found: Verify that the module exists and is not corrupted.
  • Invalid syntax: Check the syntax of the command carefully.

Integration

New-Module can be combined with other PowerShell commands to create more complex scripts and tools. For example, you can use New-Module to create a module that contains functions for managing a specific application.

  • Import-Module
  • Export-Module
  • Remove-Module