Remove-Module
移除目前工作階段的模組。
Syntax
Remove-Module
[-Name] <String[]>
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-Module
[-FullyQualifiedName] <ModuleSpecification[]>
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Remove-Module
[-ModuleInfo] <PSModuleInfo[]>
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
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 模組。
參數
-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
指定要移除之模組的完整名稱。
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]::結束) -Action $OnRemoveScript