Skip to content

Commit d8239d7

Browse files
authored
Add example for OnRemove (#10267)
1 parent 90480e1 commit d8239d7

File tree

4 files changed

+173
-96
lines changed

4 files changed

+173
-96
lines changed

reference/5.1/Microsoft.PowerShell.Core/Remove-Module.md

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 12/12/2022
5+
ms.date: 07/10/2023
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/remove-module?view=powershell-5.1&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Remove-Module
@@ -23,7 +23,8 @@ Remove-Module [-Name] <String[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameter
2323
### FullyQualifiedName
2424

2525
```
26-
Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
26+
Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm]
27+
[<CommonParameters>]
2728
```
2829

2930
### ModuleInfo
@@ -38,10 +39,10 @@ The `Remove-Module` cmdlet removes the members of a module, such as cmdlets and
3839
current session.
3940

4041
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.
4243

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.
4546

4647
## EXAMPLES
4748

@@ -92,9 +93,10 @@ The command uses a pipeline operator (`|`) to send the module names to `Remove-M
9293

9394
The **Verbose** messages show the items that are removed. The messages differ because the
9495
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.
9698

97-
### Example 4: Remove a module by using ModuleInfo
99+
### Example 4: Remove a module using ModuleInfo
98100

99101
```powershell
100102
$a = Get-Module BitsTransfer
@@ -103,6 +105,32 @@ Remove-Module -ModuleInfo $a
103105

104106
This command uses the **ModuleInfo** parameter to remove the BitsTransfer module.
105107

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+
106134
## PARAMETERS
107135

108136
### -Force
@@ -158,9 +186,9 @@ Accept wildcard characters: False
158186

159187
### -ModuleInfo
160188

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`.
164192

165193
```yaml
166194
Type: System.Management.Automation.PSModuleInfo[]
@@ -209,7 +237,7 @@ Accept wildcard characters: False
209237

210238
### -WhatIf
211239

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.
213241

214242
```yaml
215243
Type: System.Management.Automation.SwitchParameter
@@ -227,7 +255,8 @@ Accept wildcard characters: False
227255

228256
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
229257
-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).
231260

232261
## INPUTS
233262

@@ -251,6 +280,9 @@ Windows PowerShell includes the following aliases for `Remove-Module`:
251280

252281
- `rmo`
253282

283+
When you remove a module, there is an event is triggered that can be used to run some cleanup code.
284+
For more details, see [Example 5](#example-5-using-the-onremove-event).
285+
254286
## RELATED LINKS
255287

256288
[Get-Module](Get-Module.md)

reference/7.2/Microsoft.PowerShell.Core/Remove-Module.md

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 12/12/2022
5+
ms.date: 07/10/2023
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/remove-module?view=powershell-7.2&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Remove-Module
@@ -23,7 +23,8 @@ Remove-Module [-Name] <String[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameter
2323
### FullyQualifiedName
2424

2525
```
26-
Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
26+
Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm]
27+
[<CommonParameters>]
2728
```
2829

2930
### ModuleInfo
@@ -38,10 +39,10 @@ The `Remove-Module` cmdlet removes the members of a module, such as cmdlets and
3839
current session.
3940

4041
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.
4243

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.
4546

4647
## EXAMPLES
4748

@@ -92,9 +93,10 @@ The command uses a pipeline operator (`|`) to send the module names to `Remove-M
9293

9394
The **Verbose** messages show the items that are removed. The messages differ because the
9495
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.
9698

97-
### Example 4: Remove a module by using ModuleInfo
99+
### Example 4: Remove a module using ModuleInfo
98100

99101
```powershell
100102
$a = Get-Module BitsTransfer
@@ -103,6 +105,32 @@ Remove-Module -ModuleInfo $a
103105

104106
This command uses the **ModuleInfo** parameter to remove the BitsTransfer module.
105107

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+
106134
## PARAMETERS
107135

108136
### -Force
@@ -158,9 +186,9 @@ Accept wildcard characters: False
158186

159187
### -ModuleInfo
160188

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`.
164192

165193
```yaml
166194
Type: System.Management.Automation.PSModuleInfo[]
@@ -209,7 +237,7 @@ Accept wildcard characters: False
209237

210238
### -WhatIf
211239

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.
213241

214242
```yaml
215243
Type: System.Management.Automation.SwitchParameter
@@ -227,7 +255,8 @@ Accept wildcard characters: False
227255

228256
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
229257
-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).
231260

232261
## INPUTS
233262

@@ -252,22 +281,8 @@ PowerShell includes the following aliases for `Remove-Module`:
252281
- All platforms:
253282
- `rmo`
254283

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).
271286

272287
## RELATED LINKS
273288

reference/7.3/Microsoft.PowerShell.Core/Remove-Module.md

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
external help file: System.Management.Automation.dll-Help.xml
33
Locale: en-US
44
Module Name: Microsoft.PowerShell.Core
5-
ms.date: 12/12/2022
5+
ms.date: 07/10/2023
66
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/remove-module?view=powershell-7.3&WT.mc_id=ps-gethelp
77
schema: 2.0.0
88
title: Remove-Module
@@ -23,7 +23,8 @@ Remove-Module [-Name] <String[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameter
2323
### FullyQualifiedName
2424

2525
```
26-
Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
26+
Remove-Module [-FullyQualifiedName] <ModuleSpecification[]> [-Force] [-WhatIf] [-Confirm]
27+
[<CommonParameters>]
2728
```
2829

2930
### ModuleInfo
@@ -38,10 +39,10 @@ The `Remove-Module` cmdlet removes the members of a module, such as cmdlets and
3839
current session.
3940

4041
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.
4243

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.
4546

4647
## EXAMPLES
4748

@@ -92,9 +93,10 @@ The command uses a pipeline operator (`|`) to send the module names to `Remove-M
9293

9394
The **Verbose** messages show the items that are removed. The messages differ because the
9495
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.
9698

97-
### Example 4: Remove a module by using ModuleInfo
99+
### Example 4: Remove a module using ModuleInfo
98100

99101
```powershell
100102
$a = Get-Module BitsTransfer
@@ -103,6 +105,32 @@ Remove-Module -ModuleInfo $a
103105

104106
This command uses the **ModuleInfo** parameter to remove the BitsTransfer module.
105107

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+
106134
## PARAMETERS
107135

108136
### -Force
@@ -158,9 +186,9 @@ Accept wildcard characters: False
158186

159187
### -ModuleInfo
160188

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`.
164192

165193
```yaml
166194
Type: System.Management.Automation.PSModuleInfo[]
@@ -209,7 +237,7 @@ Accept wildcard characters: False
209237

210238
### -WhatIf
211239

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.
213241

214242
```yaml
215243
Type: System.Management.Automation.SwitchParameter
@@ -227,7 +255,8 @@ Accept wildcard characters: False
227255

228256
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
229257
-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).
231260

232261
## INPUTS
233262

@@ -252,22 +281,8 @@ PowerShell includes the following aliases for `Remove-Module`:
252281
- All platforms:
253282
- `rmo`
254283

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).
271286

272287
## RELATED LINKS
273288

0 commit comments

Comments
 (0)