共用方式為


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 一般參數,取得所移除之成員的詳細資訊。

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