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

此命令會 從目前的會話中移除 BitsTransferPSDiagnostics 模組。

命令會使用管線運算子 (|) 將模組名稱傳送至 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 之前先提示您確認。

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

此值可以是模組名稱、完整模組規格或模組檔案的路徑。

當值為路徑時,路徑可以是完整或相對路徑。 相對於包含 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

輸入

String

您可以使用管線將模組名稱傳送至此 Cmdlet。

PSModuleInfo

您可以使用管線將模組物件傳送至此 Cmdlet。

輸出

None

此 Cmdlet 不會傳回任何輸出。

備註

PowerShell 包含下列的 Remove-Module別名:

  • 所有平臺:
    • rmo

當您移除模組時,會觸發事件,可用來執行一些清除程序代碼。 如需詳細資訊,請參閱 範例 5