Submit Express Local SEO Traffic Simulator

Amazon


How do You Lock a File in Windows to Block Deletion or Overwriting?

How do You Lock a File in Windows to Block Deletion or Overwriting?

how-do-you-lock-a-file-in-windows-to-block-overwriting-or-deletion-00
While there are plenty of third-party programs to accomplish a task, sometimes you just want to use a built-in method to do the same thing. Today’s SuperUser Q&A post helps a reader find an awesome built-in solution to keep his files from being deleted or overwritten.
Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.
Notepad screenshot courtesy of Dan (SuperUser).

The Question

SuperUser reader boboes wants to know if there is a built-in method in Windows 7 and newer editions to lock a file and keep it from being overwritten or deleted:
Is there a way to lock a file with in-house means so that it cannot be deleted or overwritten? I need to temporarily lock files while testing copy scripts in order to check the error handling in those scripts. Until Windows XP, I would load a file in debug.exe to lock it. Is there a way to do this in Windows 7 and newer editions?
I know that there are programs for doing this. My question is if there is a built in mechanism in Windows 7 and newer editions. Sometimes I have to check a script on a PC and do not want to install new programs for that.
There are also some good suggestions here: How to purposefully exclusively lock a file? Unfortunately, they require 3rd party tools or changing the file to a locked state.
Is there a built-in method for doing this in Windows 7 and newer editions?

The Answer

SuperUser contributors Dan and Breakthrough have the answer for us. First up, Dan:
I think PowerShell is probably the neatest way to accomplish this. Something like the following:
how-do-you-lock-a-file-in-windows-to-block-overwriting-or-deletion-01
While paused, the above script causes the following prompt when attempting to open up “myfile.txt”:
how-do-you-lock-a-file-in-windows-to-block-overwriting-or-deletion-02