Remove-Module
從目前的工作階段中移除模組。
語法
name
Remove-Module
[-Name] <String[]>
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
FullyQualifiedName
Remove-Module
[-FullyQualifiedName] <ModuleSpecification[]>
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
ModuleInfo
Remove-Module
[-ModuleInfo] <PSModuleInfo[]>
[-Force]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Description
Remove-Module cmdlet 會將模組的成員(如 cmdlets 和函式)從目前會話中移除。
若模組包含組件(),.dll則該組件實作的所有成員會被移除,但組裝體不會卸載。
這個指令檔不會卸載模組或刪除它。 它只影響目前的 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 common 參數來取得被移除成員的詳細資訊。
冗長訊息顯示被移除的項目。 訊息的不同之處在於 BitsTransfer 模組包含一個實作其指令點的組合語言,以及一個巢狀模組,擁有自己的組合語言。 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 scriptblock 在 PowerShell 會話結束時執行。
對於腳本模組,你可以把這些程式碼加入檔案, .psm1 或放在模組清單的 ScriptsToProcess 屬性中列出的啟動腳本中。
參數
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | cf |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Force
表示此指令小程式移除唯讀模組。 預設情況下,只 Remove-Module 移除讀寫模組。
ReadOnly 與 ReadWrite 值會儲存在模組的 AccessMode 屬性中。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-FullyQualifiedName
此值可以是模組名稱、完整模組規格或模組檔案的路徑。
當值為路徑時,路徑可以是完整或相對路徑。 相對於包含 using 語句的腳本,會解析相對路徑。
當值是名稱或模組規格時,PowerShell 會搜尋指定模組 PSModulePath。
模組規格是具有下列索引鍵的哈希表。
-
ModuleName- 必要 指定模組名稱。 -
GUID- 可選 指定模組的 GUID。 - 同時 必要 至少指定下列三個鍵中的一個。
-
ModuleVersion- 指定模組的最低可接受的版本。 -
MaximumVersion- 指定模組的最大可接受的版本。 -
RequiredVersion- 指定模組的確切必要版本。 這無法與其他版本金鑰搭配使用。
-
參數屬性
| 類型: | |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
FullyQualifiedName
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-ModuleInfo
指定要移除的模組物件。 輸入包含 PSModuleInfo 物件或指令取得模組物件的變數,例如 Get-Module 指令。 你也可以將模組物件管道接到 Remove-Module。
參數屬性
| 類型: | |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ModuleInfo
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Name
指定要移除的模組名稱。 允許使用通配符字元。 你也可以將名稱字串接到 Remove-Module。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
name
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 該 Cmdlet 未被執行。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 無線 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
String
你可以把模組名稱輸入這個指令檔。
PSModuleInfo
你可以將模組物件管道(pipe)連接到這個 cmdlet。
輸出
None
此 Cmdlet 不會傳回任何輸出。