Remove-Module

删除当前会话中的模块。

语法

Remove-Module
      [-Name] <String[]>
      [-Force]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Remove-Module
      [-FullyQualifiedName] <ModuleSpecification[]>
      [-Force]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]
Remove-Module
      [-ModuleInfo] <PSModuleInfo[]>
      [-Force]
      [-WhatIf]
      [-Confirm]
      [<CommonParameters>]

说明

Remove-Module cmdlet 可从当前会话中删除模块的成员,如 cmdlet 和函数。

如果模块包含某个程序集 (.dll),则将删除由该程序集实现的所有成员,但不会卸载该程序集。

此 cmdlet 不会将该模块从计算机中卸载或删除。 它仅影响当前的 PowerShell 会话。

示例

示例 1:删除模块

Remove-Module -Name "BitsTransfer"

此命令将从当前会话中删除 BitsTransfer 模块。

示例 2:删除所有模块

Get-Module | Remove-Module

此命令将从当前会话中删除所有模块。

示例 3:通过使用管道删除模块

"FileTransfer", "PSDiagnostics" | Remove-Module -Verbose

VERBOSE: Performing operation "Remove-Module" on Target "filetransfer (Path: 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\filetransfer\filetransfer.psd1')".
VERBOSE: Performing operation "Remove-Module" on Target "Microsoft.BackgroundIntelligentTransfer.Management (Path: 'C:\Windows\assembly\GAC_MSIL\Microsoft.BackgroundIntelligentTransfer.Management\1.0.0.0__31bf3856ad364e35\Microsoft.BackgroundIntelligentTransfe
r.Management.dll')".
VERBOSE: Performing operation "Remove-Module" on Target "psdiagnostics (Path: 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\psdiagnostics\psdiagnostics.psd1')".
VERBOSE: Removing imported function 'Start-Trace'.
VERBOSE: Removing imported function 'Stop-Trace'.
VERBOSE: Removing imported function 'Enable-WSManTrace'.
VERBOSE: Removing imported function 'Disable-WSManTrace'.
VERBOSE: Removing imported function 'Enable-PSWSManCombinedTrace'.
VERBOSE: Removing imported function 'Disable-PSWSManCombinedTrace'.
VERBOSE: Removing imported function 'Set-LogProperties'.
VERBOSE: Removing imported function 'Get-LogProperties'.
VERBOSE: Removing imported function 'Enable-PSTrace'.
VERBOSE: Removing imported function 'Disable-PSTrace'.
VERBOSE: Performing operation "Remove-Module" on Target "PSDiagnostics (Path: 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\psdiagnostics\PSDiagnostics.psm1')".

此命令从当前会话中删除 BitsTransferPSDiagnostics 模块。

该命令使用管道运算符 (|) 将模块名称发送到 Remove-Module。 它使用 Verbose 通用参数来获取有关要删除的成员的详细信息。

Verbose 消息显示所删除的项。 这些消息会有所不同,因为 BitsTransfer 模块包含一个实现其 cmdlet 的程序集,以及一个拥有自己的程序集的嵌套模块。 PSDiagnostics 模块包含用于导出函数的模块脚本文件 (.psm1)。

示例 4:使用 ModuleInfo 删除模块

$a = Get-Module BitsTransfer
Remove-Module -ModuleInfo $a

此命令使用 ModuleInfo 参数删除 BitsTransfer 模块。

示例 5:使用 OnRemove 事件

删除模块时,该模块会触发一个事件触发器,后者允许模块对删除操作做出反应并执行一些清理任务,例如释放资源。

$OnRemoveScript = {
    # perform cleanup
    $cachedSessions | Remove-PSSession
}
$ExecutionContext.SessionState.Module.OnRemove += $OnRemoveScript

$registerEngineEventSplat = @{
    SourceIdentifier = ([System.Management.Automation.PsEngineEvent]::Exiting)
    Action = $OnRemoveScript
}
Register-EngineEvent @registerEngineEventSplat

$OnRemoveScript 变量包含用于清理资源的脚本块。 通过将该脚本块分配给 $ExecutionContext.SessionState.Module.OnRemove 来注册它。 还可以使用 Register-EngineEvent 在 PowerShell 会话结束时执行该脚本块。

对于基于脚本的模块,请将此代码添加到 .PSM1 文件中,或将其放入模块清单的 ScriptsToProcess 属性中列出的启动脚本中。

参数

-Confirm

提示你在运行 cmdlet 之前进行确认。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Force

指示此 cmdlet 将删除只读模块。 默认情况下,Remove-Module 只删除读写模块。

ReadOnlyReadWrite 值存储在模块的 AccessMode 属性中。

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-FullyQualifiedName

该值可以是模块名称、完整模块规范或模块文件的路径。

如果该值是路径,则可以是完全限定路径或相对路径。 相对路径相对于包含 using 语句的脚本进行解析。

当该值为名称或模块规范时,PowerShell 会在 PSModulePath 中搜索指定的模块。

模块规范是具有以下键的哈希表。

  • ModuleName - 必需指定模块名称。
  • GUID - 可选指定模块的 GUID。
  • 必需指定以下三个键中的至少一个。
    • ModuleVersion - 指定模块的最低可接受版本。
    • MaximumVersion - 指定模块的最低可接受版本。
    • RequiredVersion - 指定模块所需的确切版本。 这不能与其他版本键一起使用。
Type:ModuleSpecification[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-ModuleInfo

指定要删除的模块对象。 输入一个包含 PSModuleInfo 对象的变量,或者可获取模块对象的命令,如 Get-Module 命令。 还可以通过管道将模块对象传递给 Remove-Module

Type:PSModuleInfo[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Name

指定要删除的模块的名称。 允许使用通配符。 还可以通过管道将名称字符串传递给 Remove-Module

Type:String[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-WhatIf

显示运行该 cmdlet 时会发生什么情况。 cmdlet 未运行。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

输入

String

可以将模块名称通过管道传递给此 cmdlet。

PSModuleInfo

管道可以将模块对象通过管道传递给此 cmdlet。

输出

None

此 cmdlet 不返回任何输出。

备注

PowerShell 包含 Remove-Module 的以下别名:

  • 所有平台:
    • rmo

删除模块时,它会触发一个事件,该事件可用于运行某些清理代码。 有关更多详细信息,请参阅示例 5