End - PowerShell
Overview
End terminates an active PowerShell session, prompting the user to confirm the operation before proceeding. It is commonly used to exit the console or to end a specific PowerShell script or script block.
Syntax
End [-Confirm] [-Force] [-Message <string>]
Options/Flags
- -Confirm: Prompts the user for confirmation before terminating the session. This is the default behavior.
- -Force: Terminates the session without prompting for confirmation.
- -Message
: Specifies a custom message to display to the user before terminating the session.
Examples
Simple Termination:
End
Forced Termination:
End -Force
Custom Message with Confirmation:
End -Confirm -Message "Are you sure you want to exit the session?"
Common Issues
- Accidental Termination: Without the
-Confirmflag,Endcan accidentally terminate the session. Use-Confirmor-Forcewith caution. - Termination of Nested Blocks:
Endonly terminates the current PowerShell block or script. To terminate nested blocks, useBreakorExit.
Integration
- With Error Handling:
Endwithin atry...catchblock can be used to prematurely terminate the session in case of an error. - In Scripts:
Endcan be used within PowerShell scripts to exit the script early under specific conditions.
Related Commands
- Exit: Alias for End.
- Break: Terminates the current PowerShell block.
- Exit-PSSession: Ends a remote PowerShell session.