Save Module - PowerShell


Overview

Save-Module exports a PowerShell module from memory to a file on disk. This enables you to create distributable modules that can be imported and used on other systems.

Syntax

Save-Module [-Name] <string> [-Path] <FilePath> [-Force] [-Verbose] [-Confirm] [-WhatIf]

Options/Flags

-Name: Specifies the name of the module to save. If not provided, the name of the current module will be used.

-Path: Specifies the path of the file to save the module to. The default path is the current directory.

-Force: Overwrites the target file if it already exists without prompting for confirmation.

-Verbose: Enables verbose output mode, providing detailed information about the save operation.

-Confirm: Prompts for confirmation before overwriting an existing file.

-WhatIf: Performs a simulation of the operation without actually executing it.

Examples

Example 1: Save a Module with Default Name and Path

Save-Module

Example 2: Save a Module with Custom Name and Path

Save-Module -Name MyModule -Path C:\Modules

Example 3: Force Overwrite an Existing File

Save-Module -Name MyModule -Path C:\Modules -Force

Common Issues

  • Permission Denied: Ensure you have sufficient permissions to save the module file to the specified path.
  • Module Not Loaded: If the module you want to save is not currently loaded, use Import-Module to load it before saving.

Integration

Save-Module can be combined with other commands to create automated module management workflows, such as:

Create a Module:

New-Module -Name MyModule
Add-Command -Module MyModule -Name MyCmdlet
Save-Module MyModule -Path C:\Modules

Export and Import Modules:

Save-Module MyModule -Path C:\Export
Import-Module -Name MyModule -Path C:\Import
  • New-Module: Creates a new PowerShell module.
  • Import-Module: Imports a PowerShell module.
  • Export-ModuleMember: Exports specific members of a module.