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.
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sivandavid
Posts: 14
Last visit: Wed Jun 21, 2023 3:45 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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 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: 14
Last visit: Wed Jun 21, 2023 3:45 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: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 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.
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked