How can I have a working "Delete on Reboot" right-click context menu option on Windows 10?

0

I've been looking for the most convenient way to tell Windows 10 to delete certain files upon rebooting. Having a right-click context menu option seems to be the most efficient solution. I've stumbled across the following registry entries that give me such an option, but on Windows 10 x64 it isn't actually deleting the files I apply it to.

[HKEY_CLASSES_ROOT\*\shell\Delete on reboot\command]
@="CMD /E:OFF /C REG ADD HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\Currentversion\\RunOnce /v \"Del %1 OnNextReboot\" /d ^\"cmd.exe /c DEL /F /Q \\\"%1\\\"\" /f\""

[HKEY_CLASSES_ROOT\*\shell\Open]

[HKEY_CLASSES_ROOT\Folder\shell\Delete on reboot\command]
@="CMD /E:OFF /C REG ADD HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\Currentversion\\RunOnce /v \"Del %1 OnNextReboot\" /d ^\"cmd.exe /c RD /S /Q \\\"%1\\\"\" /f\""

Apparently this registry code has been floating around the internet for a long time, and the most updated information I could find about it was someone saying that it worked for them on Windows 7. Is there some way I can modify it to work on Windows 10?

Thank you.

vertigoelectric

Posted 2016-09-03T00:55:27.697

Reputation: 505

Answers

2

My solution was made possible through w32sh's help. The solution I'm sharing in this answer is after following his instructions first.

In the end, what I ended up doing here was combining a command for taking ownership of the file and using movefile.exe on it.

I created a batch file (I called it "movefile_custom.bat") with the following:

takeown /f %1 /r /d y && icacls %1 /grant administrators:F /t
takeown /f %1 && icacls %1 /grant administrators:F
"C:\Windows\movefile.exe" %1 ""

(NOTE: The right-click entry only works on files, so the first line that handles directories is probably unnecessary, but I left it in for now anyway. It doesn't hurt anything)

I then downloaded BAT to EXE Converter and used it to (you guessed it) convert my "movefile_custom.bat" to "movefile_custom.exe". The converter has an administrator option for the EXE.

I edited the registry entry to point to my new "movefile_custom.exe" file instead of the base "movefile.exe".

Works great now.

vertigoelectric

Posted 2016-09-03T00:55:27.697

Reputation: 505

1

Download SysInternals Movefile.exe and place it in C:\Windows folder.

Right-click Movefile.exe and click Properties. Select the Compatibility tab, click "Change settings for all users" Enable "Run this program as an administrator" Click OK, OK.

Then create a .REG file as below.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\deleteonreboot]
@="Delete File on Reboot"

[HKEY_CLASSES_ROOT\*\shell\deleteonreboot\command]
@="\"C:\\Windows\\movefile.exe\" \"%1\" \"\""

Double-click the REG file to apply the settings.

This adds Delete on reboot option in the right-click menu.

w32sh

Posted 2016-09-03T00:55:27.697

Reputation: 8 611

Thank you! Unfortunately this isn't working. Everything gets entered into the registry just fine, and I know that the movefile.exe is being executed when I select the right-click option, but the files just aren't being deleted after rebooting. Actually, let me try manually using movefile.exe on a file and see if that works... I'll report back asap. – vertigoelectric – 2016-09-07T14:47:31.440

Ok. Let's know how it goes.. It worked fine here when I tested. – w32sh – 2016-09-07T14:53:43.310

It's not deleting the files. However, now that I understand how to use the registry to apply basically any executable/script to a file using right-click, perhaps I can use something other than movefile.exe to do this... but still... I'd like to figure out why it's not working. – vertigoelectric – 2016-09-07T14:56:01.970

Ah, in my test batch file I've added "pause" and now see that I'm getting errors. I'm testing two files, one of them has a special character. The normal file returns "Error: 5", the other file returns "Error: 123". I'm going to research these. – vertigoelectric – 2016-09-07T15:00:02.933

Perhaps the file you're trying to delete is owned by "TrustedInstaller"? – w32sh – 2016-09-07T15:00:03.723

Error 5 is access denied. Someone suggested taking ownership of the file. I do have a right-click option for that. However I'd prefer to not make this multiple steps (hence making it a right-click option to begin with). I don't understand why I'm constantly having to take ownership of files. – vertigoelectric – 2016-09-07T15:03:44.850

This can happen if Ti is the owner. You can combine takeown + movefile.exe (using a vbscript or PS) and implement it in the context menu. – w32sh – 2016-09-07T15:07:17.480

I finally got it working. I don't have enough experience with VBS or PS, so what I did was create a batch file containing the commands for takown and movefile. I then used a BAT to EXE converter to make it an EXE with admin rights, and pointed to that in the registry entry. It still doesn't delete the files with the special character, but I'll just stop using that character for now. I've upvoted your answer, but feel I should post what I did as its own answer. Thank you so much for your help! – vertigoelectric – 2016-09-07T15:46:19.753

0

This page describes how to use Group Policy to run custom scripts on shutdown/reboot, or logoff: https://technet.microsoft.com/en-us/library/cc753404(v=ws.11).aspx

I've used it in the past on previous versions of Windows, and although the page says only up to Win 7/2012, I know that similar policies also work on 10. I'd wager this would too.

There are also third-party services that you can configure to run a script on shutdown/reboot - such as SRVSTART (http://www.rozanski.org.uk/services), although it's not very straightforward in that context and not it's primary purpose.

Jim

Posted 2016-09-03T00:55:27.697

Reputation: 106

Thanks for the input. I do know there are various ways to automate scripts on shutdown/reboot, but I'm not quite sure how I might implement the "Delete on Reboot" right click item into those. – vertigoelectric – 2016-09-03T03:45:17.147

@vertigoelectric Sorry, I misunderstood your question, my bad. – Jim – 2016-09-03T21:54:13.173

@w32sh, Thanks! Yeah, if you could help me with getting that added to the menu I'd be very grateful. Be sure to post it as an answer so I can select it. – vertigoelectric – 2016-09-04T13:00:44.620

@bubbles, no worries! – vertigoelectric – 2016-09-04T13:00:57.743