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 -Confirm flag, End can accidentally terminate the session. Use -Confirm or -Force with caution.
  • Termination of Nested Blocks: End only terminates the current PowerShell block or script. To terminate nested blocks, use Break or Exit.

Integration

  • With Error Handling: End within a try...catch block can be used to prematurely terminate the session in case of an error.
  • In Scripts: End can be used within PowerShell scripts to exit the script early under specific conditions.
  • Exit: Alias for End.
  • Break: Terminates the current PowerShell block.
  • Exit-PSSession: Ends a remote PowerShell session.