Self-Deleting Script turned into Exe?

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

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 9 years and 7 months 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.
User avatar
surena
Posts: 1
Last visit: Wed Jul 05, 2023 12:08 pm

Self-Deleting Script turned into Exe?

Post by surena »

Normally, I use the command:

Remove-Item $MyINvocation.InvocationName

to self-delete a script after it's ran. But when I convert it into an EXE, I get the error:

Remove-Item: Cannot bind argument to parameter 'Path' because it is an empty string.

This works when it is a PS1, but no longer works as an EXE.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Self-Deleting Script turned into Exe?

Post by davidc »

$MyINvocation.InvocationName will not work in a packaged script.

You can look at the Get-ScriptDirectory snippet so you can get an idea how to get the script path.

Here is what you would use:
PowerShell Code
Double-click the code block to select all.
if($hostinvocation -ne $null)
{
	$hostinvocation.MyCommand.path
}
else
{
	$script:MyInvocation.MyCommand.Path
}
Note: An executable can't delete itself while running in Windows.

David
David
SAPIEN Technologies, Inc.
This topic is 9 years and 7 months 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.