PowerShell: Self Destruct Function

Overview

The following function is designed to delete the script file it resides in as soon as it is executed. This makes it ideal for situations where you need the script to run only once, ensuring that it cannot be accidentally or intentionally run again in the future.

PowerShell Code
Function Self-Destruct
{
    $CurrentScriptFullPathName = $MyInvocation.MyCommand.Definition
    $CurrentScriptName = $MyInvocation.MyCommand.Name
    
    Remove-Item $CurrentScriptFullPathName
    Start-Sleep 3
}

Post a Comment

0 Comments