Search code examples
filetemppowershell-4.0

How to delete my temp folder after Powershell is forced to stop


I have a security question regarding a script that I have. My script creates two temporary CSV files, and after I run the script those files are deleted. Now, when the script crashes or the user stops it, those files remain in to the folder. How can I make sure that those files will get deleted if this happens?

I was thinking about using the windows temp folder "$TempDir = [System.IO.Path]::GetTempPath()", but this will not make any difference since the temp folder is renewed after boot time.

Any thoughts/suggestions?


Solution

  • One solution to your problem may be to write the temporary files to a location that only a limited number of people have access too.

    Then, if the existence of these files may affect future executions of the script then a good practice would be to first check for left-overs from previous executions of the scripts, and if there are start the script with cleaning these up.