script file "self destruct" on compiled EXE file

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.
Locked
User avatar
sivandavid
Posts: 11
Last visit: Mon Feb 06, 2023 11:53 am

script file "self destruct" on compiled EXE file

Post by sivandavid »

I have a script that contains 2 files:
users.csv
script.ps1
I transfer both files to client computer and run the script.ps1
at the end of the script it runs these commands:
Remove-Item .\users.csv -Force
Remove-Item $PSCommandPath -Force

the result is that when it finished running it deletes both files from client computer as part of the script.
when I try to compile the PS1 file to EXE using Sapien powershell studio it is able to delete the CSV file but not the script file (EXE file)
is there a way to force him to do so ?
thanks
David
jvierra
Posts: 15382
Last visit: Fri Mar 24, 2023 6:18 pm
Answers: 29
Has voted: 4 times
Been upvoted: 32 times

Re: script file "self destruct" on compiled EXE file

Post by jvierra »

No. The file you are running is in the EXE so that would make no sense. There would be no PS1 file in the folder. Why would you copy a PS1 and embed the same file into an EXE?
User avatar
sivandavid
Posts: 11
Last visit: Mon Feb 06, 2023 11:53 am

Re: script file "self destruct" on compiled EXE file

Post by sivandavid »

I don't copy both the ps1 and EXE
I want to use instead of
users.csv
script.ps1

to compile the PS1 to EXE and then use only
users.csv
script.exe

but the command (from within the EXE)
Remove-Item $PSCommandPath -Force

don't delete the the EXE file

any idea how to force it ? or use a different command ?
User avatar
Alexander Riedel
Posts: 8221
Last visit: Tue Mar 28, 2023 10:02 am
Answers: 12
Been upvoted: 30 times

Re: script file "self destruct" on compiled EXE file

Post by Alexander Riedel »

You cannot delete an executable file that is in use. Not from the inside.
Here is a discussion on the subject:
https://stackoverflow.com/questions/160 ... executable
Alexander Riedel
SAPIEN Technologies, Inc.
Locked