The documentation for try-catch-finally says:
A Finally block runs even if you use CTRL+C to stop the script. A Finally
block also runs if an Exit keyword stops the script from within a Catch
block.
See the following example. Run it and cancel it by pressing ctrl-c
.
try
{
while($true)
{
"Working.."
Start-Sleep -Seconds 1
}
}
finally
{
write-host "Ended work."
}
Related Contents:
- What’s the best way to determine the location of the current PowerShell script?
- Save hash table in PowerShell object notation (PSON)
- When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors
- How to perform keystroke inside powershell?
- Delete files older than 15 days using PowerShell
- How to get disk capacity and free space of remote computer
- How do I write to standard error in PowerShell?
- Is there a way to make a PowerShell script work by double clicking a .ps1 file?
- Redirecting standard input\output in Windows PowerShell
- Recursive file search using PowerShell
- Get remote registry value
- How to get an MD5 checksum in PowerShell
- Copy file remotely with PowerShell
- Executing an EXE file using a PowerShell script
- Obtaining ExitCode using Start-Process and WaitForExit instead of -Wait
- How to speed up Powershell Get-Childitem over UNC
- Using Invoke-Command -ScriptBlock on a function with arguments
- How to properly -filter multiple strings in a PowerShell copy script
- Powershell script to see currently logged in users (domain and machine) + status (active, idle, away)
- How do I get only directories using Get-ChildItem?
- PowerShell – Decode System.Security.SecureString to readable password
- PowerShell script to check the status of a URL
- Methods to hex edit binary files via Powershell
- Launch Elevated CMD.exe from Powershell
- Powershell – Reboot and Continue Script
- Hashtables and key order
- How do I include a locally defined function when using PowerShell’s Invoke-Command for remoting?
- In what scenario was Invoke-Expression designed to be used?
- PowerShell, formatting values in another culture
- How can I write/create a powershell script verbatim, from another powershell script?
- Why do PowerShell comparison operators not enumerate collections of size 1?
- Using a Variable (PowerShell) inside of a command
- How can I use powershell to run through an installer?
- Add a where-object on a table construct?
- What’s the difference between “Write-Host”, “Write-Output”, or “[console]::WriteLine”?
- How can I split a text file using PowerShell?
- How to process a file in PowerShell line-by-line as a stream
- CMD pipe different form Powershell pipe?
- Powershell color formatting with format operator
- Reload the path in PowerShell
- Saving credentials for reuse by powershell and error ConvertTo-SecureString : Key not valid for use in specified state
- Get current user’s credentials object in Powershell without prompting
- How can I start Powershell script as noprofile in Visual Studio Code
- How to reference .NET assemblies using PowerShell
- Iterate over PSObject properties in PowerShell
- Why does “Get-ChildItem -File | Get-FileHash” work?
- How to pass credentials to the Send-MailMessage command for sending emails
- Powershell output column width
- Ignoring an errorlevel != 0 in Windows PowerShell (ISE)
- Run Executable from Powershell script with parameters