Self-Destruct

countdownSometimes we need want to play secret agents, sometimes we just want to remove our trail. Remote execution is one thing and then, sometimes, for non-legitimate purpose we need a local script to be executed and not to stay there then. Here’s a script template that will self destruct after its execution.

param([string]$countDown=5)
function CountDown($waitSeconds) 
{
$startTime=Get-Date
$endTime=$startTime.addSeconds($waitSeconds)
$timeSpan=New-Timespan $startTime $endTime
While ($timeSpan -gt 0)
    {
    $timeSpan=New-Timespan $(Get-Date) $endTime
        Write-Host "`r".padright(40," ") -NoNewline
        Write-Host $([string]::Format("`rTime Remaining: {0:d2}:{1:d2}:{2:d2}", $timeSpan.hours, $timeSpan.minutes, $timeSpan.seconds)) -NoNewline -ForegroundColor DarkYellow
        Start-Sleep 1
    }
}

<#
INSERT HERE THE SCRIPT ACTIONS
#>

Write-Host "This script will self destruct in $countDown seconds...`r`n" -ForegroundColor DarkRed
Countdown $countDown
Remove-Item $PSCommandPath
Write-Host "`r`n`r`nThis script has self destructed. Bye`r`n" -ForegroundColor DarkRed

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top