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 通用参数获取有关已删除的成员的详细信息。
详细 消息显示已删除的项目。 消息有所不同,因为 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 之前,提示你进行确认。
类型: | SwitchParameter |
别名: | cf |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-Force
指示此 cmdlet 删除只读模块。 默认情况下,Remove-Module
仅删除读写模块。
ReadOnly 和 ReadWrite 值存储在模块的 AccessMode 属性中。
类型: | SwitchParameter |
Position: | Named |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
-FullyQualifiedName
该值可以是模块名称、完整模块规范或模块文件的路径。
如果值为路径,则路径可以完全限定或相对。 相对于包含 using 语句的脚本解析相对路径。
当该值为名称或模块规范时,PowerShell 会在 PSModulePath 中搜索指定的模块。
模块规范是具有以下键的哈希表。
-
ModuleName
- 必需 指定模块名称。 -
GUID
- 可选 指定模块的 GUID。 - 它还 必需 指定以下三个键中的至少一个。
-
ModuleVersion
- 指定模块的最低可接受版本。 -
MaximumVersion
- 指定模块的最大可接受版本。 -
RequiredVersion
- 指定模块的确切所需版本。 这不能与其他版本密钥一起使用。
-
类型: | ModuleSpecification[] |
Position: | 0 |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-ModuleInfo
指定要删除的模块对象。 输入一个变量,其中包含 PSModuleInfo 对象或获取模块对象的命令,例如 Get-Module
命令。 还可以通过管道将模块对象传递给 Remove-Module
。
类型: | PSModuleInfo[] |
Position: | 0 |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | False |
-Name
指定要删除的模块的名称。 允许使用通配符。 还可以通过管道将名称字符串传递给 Remove-Module
。
类型: | String[] |
Position: | 0 |
默认值: | None |
必需: | True |
接受管道输入: | True |
接受通配符: | True |
-WhatIf
显示 cmdlet 运行时会发生什么情况。 该 cmdlet 未运行。
类型: | SwitchParameter |
别名: | wi |
Position: | Named |
默认值: | False |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
可以通过管道将模块名称传递给此 cmdlet。
可以通过管道将模块对象传递给此 cmdlet。
输出
None
此 cmdlet 不返回任何输出。
备注
PowerShell 包含以下 Remove-Module
别名:
- 所有平台:
rmo
删除模块时,会触发一个事件,该事件可用于运行一些清理代码。 有关详细信息,请参阅 示例 5。