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>]
说明
cmdlet Remove-Module
从当前会话中删除模块的成员,例如 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 模块。
示例 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
仅删除读写模块。
ReadOnly 和 ReadWrite 值存储在模块的 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 |
输入
可以通过管道将模块名称传递给此 cmdlet。
可以通过管道将模块对象传递给此 cmdlet。
输出
None
此 cmdlet 不返回任何输出。
备注
PowerShell 包含以下别名 Remove-Module
:
- 所有平台:
rmo
删除模块时,会触发一个事件,该事件可用于运行一些清理代码。 有关更多详细信息,请参阅 示例 5。