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')".

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

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

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

示例 4:通过使用 ModuleInfo 删除模块

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

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

参数

-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

指定要删除的模块的完全限定名称。

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

输入

System.String, System.Management.Automation.PSModuleInfo

可以通过管道将模块名称和模块对象传递给 Remove-Module

输出

None

此 cmdlet 将不生成任何输出。

备注

删除模块时,模块上有一个将执行的事件。 此事件允许模块对被删除做出反应,并执行一些清理,例如释放资源。 例如:

$OnRemoveScript = {

# 执行清理

$cachedSessions |Remove-PSSession

}

$ExecutionContext.SessionState.Module.OnRemove += $OnRemoveScript

对于完全一致性,对 PowerShell 进程的关闭做出反应可能也很有用:

Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $OnRemoveScript