How to delete my .EXE script just before exit it?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
peracchi
Posts: 2
Last visit: Fri Nov 12, 2021 7:28 am

How to delete my .EXE script just before exit it?

Post by peracchi »

Hi everbody!

Please, can someone help me with this issue: I want to delete my script as his "last action".

It works nice when I use the .ps1 file.

But after build the .exe and run it, it appears to not be able to delete itself.

Any ideas?

jvierra
Posts: 14987
Last visit: Tue Nov 30, 2021 11:26 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: How to delete my .EXE script just before exit it?

Post by jvierra »

You can't delete any executable while it is running. The OS owns and controls teh EXE whie it is running. You can schedule a one-shot task to delete the exe after it ends.

peracchi
Posts: 2
Last visit: Fri Nov 12, 2021 7:28 am

Re: How to delete my .EXE script just before exit it?

Post by peracchi »

Thanks!

Will try this approach. (as soon as I discover how to do it from the PowerShell script) :lol:

User avatar
owinsloe
Posts: 118
Last visit: Tue Nov 30, 2021 7:24 pm

Re: How to delete my .EXE script just before exit it?

Post by owinsloe »

You could try this..

#
# do stuff
#
# remove self
$Self = (Get-Process -Id $pid).Path
$Exe = 'powershell.exe'
$ExeArgs = '-command {remove-item '+$Self+' -force}'
start-process powershell -ArgumentList "sleep 1; $Exe $ExeArgs" -WindowStyle hidden

jvierra
Posts: 14987
Last visit: Tue Nov 30, 2021 11:26 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: How to delete my .EXE script just before exit it?

Post by jvierra »

owinsloe wrote:
Tue Nov 23, 2021 4:55 pm
You could try this..

#
# do stuff
#
# remove self
$Self = (Get-Process -Id $pid).Path
$Exe = 'powershell.exe'
$ExeArgs = '-command {remove-item '+$Self+' -force}'
start-process powershell -ArgumentList "sleep 1; $Exe $ExeArgs" -WindowStyle hidden
This is the original issue. You cannot delete a process that is running. Read the first response. That is the only method that can be used. Your method will execute before the current process has exited and the file will still be locked by the system.

User avatar
owinsloe
Posts: 118
Last visit: Tue Nov 30, 2021 7:24 pm

Re: How to delete my .EXE script just before exit it?

Post by owinsloe »

@jverra,
It does work, I have tried it.
The parent of the first powershell is the executable but the second powershell from the start-process launches after the primary executable has completed).

Firing order..
2021/11/24 16:15:24.483 B test "C:\cardinal\bin\test.exe"
2021/11/24 16:15:25.277 B powershell "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" sleep 1; powershell.exe -command {remove-item C:\cardinal\bin\test.exe -force}
2021/11/24 16:15:25.309 E test exit=0 "C:\cardinal\bin\test.exe"
2021/11/24 16:15:26.596 B powershell "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -encodedCommand cgBlAG0AbwB2AGUALQBpAHQAZQBtACAAQwA6AFwAYwBhAHIAZABpAG4AYQBsAFwAYgBpAG4AXAB0AGUAcwB0AC4AZQB4AGUAIAAtAGYAbwByAGMAZQA= -inputFormat xml -outputFormat text
2021/11/24 16:15:26.849 E powershell exit=0 "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" -encodedCommand cgBlAG0AbwB2AGUALQBpAHQAZQBtACAAQwA6AFwAYwBhAHIAZABpAG4AYQBsAFwAYgBpAG4AXAB0AGUAcwB0AC4AZQB4AGUAIAAtAGYAbwByAGMAZQA= -inputFormat xml -outputFormat text
2021/11/24 16:15:26.874 E powershell exit=0 "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe" sleep 1; powershell.exe -command {remove-item C:\cardinal\bin\test.exe -force}

jvierra
Posts: 14987
Last visit: Tue Nov 30, 2021 11:26 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: How to delete my .EXE script just before exit it?

Post by jvierra »

Sounds like too many operations when you can just schedule a one shot and exit the program. THat does everything you are doing in one line.

Why write ten times as much code as necessary?

User avatar
owinsloe
Posts: 118
Last visit: Tue Nov 30, 2021 7:24 pm

Re: How to delete my .EXE script just before exit it?

Post by owinsloe »

I have used TS in the past to divorce processes and have not always found it that reliable and you have to deal with credentials. But thats fine, the good thing with powershell and MS...you have loads of choices.

jvierra
Posts: 14987
Last visit: Tue Nov 30, 2021 11:26 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: How to delete my .EXE script just before exit it?

Post by jvierra »

owinsloe wrote:
Wed Nov 24, 2021 12:07 pm
I have used TS in the past to divorce processes and have not always found it that reliable and you have to deal with credentials. But thats fine, the good thing with powershell and MS...you have loads of choices.
Not true under the question was asked and, in both cases, you would require credentials. The task-based method is the easiest, most reliable method except for one other.

In the end this would only be an issue for hackers attempting to bypass security as an installer can do all of this withot the need to delete anything.

I, and all software engineers oppose these hacking methods as they are mostly used to distribute malware and violate all basic principles of secure and reliable software development.

Often untrained techs learning scripting cook up these schemes as if they are new. They are not new. They are old schemes that are trivial and come in many forms. Learning correct methods that are compliant with all vendor APIs and directives are where all non-engineers should focus to avoid creating security holes in systems.

Happy Thanksgiving (in the US), good luck, take care and avoid poppycock methods by learning basic software engineering and current security methods.

Post Reply