# Clean up before exiting script

**URL:** <https://forums.powershell.org/t/clean-up-before-exiting-script/20605>\
**Category:** PowerShell Help\
**Created:** [October 10, 2022, 2:29pm UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605 "2022-10-10T14:29:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matbli](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@matbli](https://forums.powershell.org/u/matbli)\
**Post date:** [October 10, 2022, 2:29pm UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605/1 "2022-10-10T14:29:06Z")

</div>

I have a script that does a few things before starting a job and then executing:

```auto
get-content $OutFile -Tail 100 -Wait

```

Resulting in a background job producing an output to a file that is tailed and displayed.  
Pressing Ctrl-C will exit the script altogether. All this works fine.

But, I would want the script to kill the job before exiting, after canceling the get-content command.

Is this possible in any way?

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [October 10, 2022, 5:48pm UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605/2 "2022-10-10T17:48:09Z")

</div>

Use cmdlet ‘Remove-Job’ to delete a background job. What have you tried so far?

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [October 10, 2022, 5:50pm UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605/3 "2022-10-10T17:50:57Z")

</div>

I think he is referencing catching the CTRL/C action and then killing the job before the script cancels ???

---

<div class="post-metadata">

**Author:** ![matbli](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@matbli](https://forums.powershell.org/u/matbli)\
**Post date:** [October 12, 2022, 8:43am UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605/4 "2022-10-12T08:43:59Z")

</div>

Yes, that is right. Right now I have a remove-job at the beginning of the script, removing any old instances of the job. But I really would like the job to be removed before the script ends.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [October 12, 2022, 8:59am UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605/5 "2022-10-12T08:59:54Z")

</div>

If you end the script with `<Ctrl>+<C>` there is no way for your script to do anything else because it does not run anymore. You would need to implement a loop inside your script checking for a cancel condition and clean up before ending itself.

---

<div class="post-metadata">

**Author:** ![matbli](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@matbli](https://forums.powershell.org/u/matbli)\
**Post date:** [October 18, 2022, 12:57pm UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605/6 "2022-10-18T12:57:59Z")

</div>

try - finally, did the trick.

```auto
try
{

  write-host Starting job.
  start-job ........
  get-content ........ -Tail 100 -Wait

}

finally
{

  write-host "Removing job, please wait ...."
  remove-job ......... -Force 2>$null
	
}

```

When entering ctr-c to exit get-content the job will be removed before the script ends.  
Just the way I want it. 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/clean-up-before-exiting-script/20605/7 "2024-05-16T20:16:28Z")

</div>


