Self-Destruction Script

As a part of some work I’m doing, I’ve decided that I need a specific script to do some final configuration for me. More to come on that in time, perhaps. The point here, however, is that a PowerShell script completes my final configuration, and when this final configuration is complete, I have no need for my script any longer. Can a PowerShell script delete itself? Sure it can.

How does a script delete itself? Easy. It’s a single line, really. However, I have a mildly more interesting example. Before you take a look at the included gif — this really does lend itself to a visual — here are the contents of my self-destruction script. It echos an indication that the script will self-destruct. At this point, it begins a countdown in seconds, from 5 to 1, before removing itself — the script file — from the file system. Take a look, and maybe, just maybe, you’ll find yourself in need of something like this sometime, too.

Write-Output -InputObject 'This script will self-destruct in 5 seconds.'

5..1 | ForEach-Object {
    If ($_ -gt 1) {
        "$_ seconds"
    } Else {
        "$_ second"
    } # End If.
    Start-Sleep -Seconds 1
} # End ForEach-Object.

Write-Output -InputObject 'Self destruction.'

# Here's the command to delete itself.
Remove-Item -Path $MyInvocation.MyCommand.Source

And here’s, the visual representation. It’s a bit small, but you should be able to determine what’s happening. The script executes on the left, and once the countdown is over, DeleteSelfScript.ps1 is removed on the right. It’s deleted itself.

Enjoy the week, and learn something new!

7 thoughts on “Self-Destruction Script

    1. tommymaynard Post author

      There is no way for the script itself to execute any final commands after the one it executes to delete itself. I have an idea, however, and I’ll update this comment if I share anything new.

      Reply
    2. Andrew

      How crazy….I came to this page a few days ago. I was just writing a script where I needed to rename a PC and then have the script restart and delete itself. Tommy, any ideas would be greatly appreciated. The only thing I can think to come up with, is having your script run another script that would wait X number of seconds before restarting.

      $PCName = read-host ‘Enter PC Name’
      Rename-Computer -NewName $PCName -DomainCredential domain\user -Restart

      Reply
      1. tommymaynard Post author

        Your idea was all I could come up with, as well. That’s even if it will work. Does the script that deletes itself wait for the completion of the other script that sleeps and then restarts the computer? If so, it’s not going to get a chance to delete itself… Maybe look into background jobs combined with only running under certain circumstances (like the first script being done). No idea if this will work, but it might be worth a minute of your time — let me know if you come up with something or if I can help!

        Reply
        1. Narcopolypse

          The old way this was accomplished back in the batch script days was by enclosing the commands in () as everything inside a command block is loaded into the command buffer before execution.
          Powershell does not write command blocks to the command buffer in this way, but we can still use the command buffer to perform a similar function.
          All we have to do is store the commands to be executed inside a variable first, since PowerShell DOES write individual commands to the command buffer before execution.
          For example:

          $DelPath = $MyInvocation.MyCommand.Source
          $DelProof = “Test 3”

          $BufferExecute=’
          “Test 1”
          Remove-Item -Path $DelPath
          “Test 2”
          $DelProof

          Invoke-Expression $BufferExecute

          As you can see, we are storing 4 commands in the “$BufferExecute” variable.
          To prove that we are able to continue executing commands after the script has deleted itself, we write “Test 2” to the console, and to prove that all previously written variables are still stored in memory, we write “Test 3” from the variable “$DelProof”.

          Reply
  1. Anonymous

    I think there is a way to restart after the deletion, all you haave to do is set a restart with timer of one second or something, i don’t know how to do it in Powershell, but you may enter into the powershell the cmd line:

    shutdown /r /t 1

    * add this line BEFORE the Remove-Item command.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *