2
2
external help file : System.Management.Automation.dll-Help.xml
3
3
Locale : en-US
4
4
Module Name : Microsoft.PowerShell.Core
5
- ms.date : 12/12/2022
5
+ ms.date : 07/10/2023
6
6
online version : https://learn.microsoft.com/powershell/module/microsoft.powershell.core/remove-module?view=powershell-7.2&WT.mc_id=ps-gethelp
7
7
schema : 2.0.0
8
8
title : Remove-Module
@@ -23,7 +23,8 @@ Remove-Module [-Name] <String[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameter
23
23
### FullyQualifiedName
24
24
25
25
```
26
- Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
26
+ Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm]
27
+ [<CommonParameters>]
27
28
```
28
29
29
30
### ModuleInfo
@@ -38,10 +39,10 @@ The `Remove-Module` cmdlet removes the members of a module, such as cmdlets and
38
39
current session.
39
40
40
41
If the module includes an assembly (` .dll ` ), all members that are implemented by the assembly are
41
- removed, but the assembly is not unloaded.
42
+ removed, but the assembly isn't unloaded.
42
43
43
- This cmdlet does not uninstall the module or delete it from the computer. It affects only the
44
- current PowerShell session.
44
+ This cmdlet doesn't uninstall the module or delete it from the computer. It affects only the current
45
+ PowerShell session.
45
46
46
47
## EXAMPLES
47
48
@@ -92,9 +93,10 @@ The command uses a pipeline operator (`|`) to send the module names to `Remove-M
92
93
93
94
The ** Verbose** messages show the items that are removed. The messages differ because the
94
95
BitsTransfer module includes an assembly that implements its cmdlets and a nested module with its
95
- own assembly. The PSDiagnostics module includes a module script file (` .psm1 ` ) that exports functions.
96
+ own assembly. The PSDiagnostics module includes a module script file (` .psm1 ` ) that exports
97
+ functions.
96
98
97
- ### Example 4: Remove a module by using ModuleInfo
99
+ ### Example 4: Remove a module using ModuleInfo
98
100
99
101
``` powershell
100
102
$a = Get-Module BitsTransfer
@@ -103,6 +105,32 @@ Remove-Module -ModuleInfo $a
103
105
104
106
This command uses the ** ModuleInfo** parameter to remove the BitsTransfer module.
105
107
108
+ ### Example 5: Using the OnRemove event
109
+
110
+ When removing a module, there is an event trigger by the module that allows a module to react to
111
+ being removed and perform some cleanup task, such as freeing resources.
112
+
113
+ ``` powershell
114
+ $OnRemoveScript = {
115
+ # perform cleanup
116
+ $cachedSessions | Remove-PSSession
117
+ }
118
+ $ExecutionContext.SessionState.Module.OnRemove += $OnRemoveScript
119
+
120
+ $registerEngineEventSplat = @{
121
+ SourceIdentifier = ([System.Management.Automation.PsEngineEvent]::Exiting)
122
+ Action = $OnRemoveScript
123
+ }
124
+ Register-EngineEvent @registerEngineEventSplat
125
+ ```
126
+
127
+ The ` $OnRemoveScript ` variable contains the script block that cleans up the resources. You register
128
+ the script block by assigning it to ` $ExecutionContext.SessionState.Module.OnRemove ` . You can also
129
+ use ` Register-EngineEvent ` to have the script block execute when the PowerShell session ends.
130
+
131
+ For script-based modules, you would add this code to the ` .PSM1 ` file or put it in a startup script
132
+ that is listed in the ** ScriptsToProcess** property of the module manifest.
133
+
106
134
## PARAMETERS
107
135
108
136
### -Force
@@ -158,9 +186,9 @@ Accept wildcard characters: False
158
186
159
187
# ## -ModuleInfo
160
188
161
- Specifies the module objects to remove. Enter a variable that contains a module object
162
- (**PSModuleInfo**) or a command that gets a module object, such as a `Get-Module` command. You can
163
- also pipe module objects to `Remove-Module`.
189
+ Specifies the module objects to remove. Enter a variable that contains a **PSModuleInfo** object or
190
+ a command that gets a module object, such as a `Get-Module` command. You can also pipe module
191
+ objects to `Remove-Module`.
164
192
165
193
` ` ` yaml
166
194
Type: System.Management.Automation.PSModuleInfo[]
@@ -209,7 +237,7 @@ Accept wildcard characters: False
209
237
210
238
# ## -WhatIf
211
239
212
- Shows what would happen if the cmdlet runs. The cmdlet is not run.
240
+ Shows what would happen if the cmdlet runs. The cmdlet isn't run.
213
241
214
242
` ` ` yaml
215
243
Type: System.Management.Automation.SwitchParameter
@@ -227,7 +255,8 @@ Accept wildcard characters: False
227
255
228
256
This cmdlet supports the common parameters : -Debug, -ErrorAction, -ErrorVariable,
229
257
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
230
- -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
258
+ -WarningAction, and -WarningVariable. For more information, see
259
+ [about_CommonParameters](https://go.microsoft.com/fwlink/?LinkID=113216).
231
260
232
261
# # INPUTS
233
262
@@ -252,22 +281,8 @@ PowerShell includes the following aliases for `Remove-Module`:
252
281
- All platforms :
253
282
- ` rmo`
254
283
255
- When removing a module, there is an event on the module that will execute.
256
- This event allows a module to react to being removed and perform some cleanup such as freeing up resources. Example :
257
-
258
- $OnRemoveScript = {
259
-
260
- \# perform cleanup
261
-
262
- $cachedSessions | Remove-PSSession
263
-
264
- }
265
-
266
- $ExecutionContext.SessionState.Module.OnRemove += $OnRemoveScript
267
-
268
- For full consistency, it might be also useful to react to the closing of the PowerShell process :
269
-
270
- Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $OnRemoveScript
284
+ When you remove a module, there is an event is triggered that can be used to run some cleanup code.
285
+ For more details, see [Example 5](#example-5-using-the-onremove-event).
271
286
272
287
# # RELATED LINKS
273
288
0 commit comments