Lock Files in Windows: Prevent Deletion & Overwriting

Leveraging Built-in Tools for File Protection
Often, users seek external software to manage tasks. However, utilizing integrated system features can provide effective solutions. A recent query on SuperUser highlights a resourceful, pre-installed method for safeguarding files against accidental deletion or overwriting.
SuperUser Q&A Spotlight
This particular question and its answer originate from SuperUser, a segment of the Stack Exchange network. Stack Exchange is a collaborative platform comprised of numerous question-and-answer websites.
The solution presented offers a convenient alternative to relying on third-party applications for basic file protection.
Illustrative Example
A screenshot of Notepad, contributed by Dan from SuperUser, visually demonstrates the discussed technique. This provides a clear reference for implementing the suggested approach.
The inherent tools within an operating system are frequently capable of addressing common user needs, offering a streamlined and efficient experience.
Exploring these built-in functionalities can often eliminate the necessity for installing additional software, reducing system clutter and potential conflicts.
File Locking in Windows 7 and Later
A SuperUser user, boboes, has inquired about the availability of a native Windows feature to prevent file overwriting or deletion.
The user specifically needs a method to temporarily secure files during the testing of copy scripts, allowing for verification of error handling procedures.
Previous Methods and Current Needs
Prior to Windows XP, utilizing debug.exe to load a file served as a means of locking it.
Boboes is seeking an equivalent solution within Windows 7 and subsequent versions, preferring to avoid the installation of third-party software, particularly when testing scripts on different machines.
Existing suggestions, as referenced in a related SuperUser thread ("How to purposefully exclusively lock a file?"), often rely on external tools or modifications to the file's state.
The Core Question
The central question remains: does Windows 7, or any of its later iterations, incorporate an intrinsic mechanism for file locking, preventing unauthorized modification or removal?
The user's preference is for a built-in solution that doesn't necessitate the use of additional programs, streamlining the testing process.
Considerations for Script Testing
Locking files during script testing is crucial for robust error handling validation.
By intentionally attempting to overwrite or delete locked files, developers can confirm that their scripts correctly identify and respond to such scenarios.
Alternative Approaches
While a direct, built-in equivalent to debug.exe may not be readily available, exploring file permissions and attributes could offer partial solutions.
However, these methods may not provide the same level of guaranteed locking as the previously used technique.
File Locking in Windows
Solutions to prevent file deletion or overwriting are offered by SuperUser community members Dan and Breakthrough. Dan initially proposes a PowerShell-based approach.
PowerShell Script for File Locking
The following PowerShell script can be utilized to lock a file. This effectively prevents other processes from accessing it.

When the script is running, attempting to open the file "myfile.txt" will trigger a prompt, as illustrated below.

Technical Details from MSDN
Breakthrough supplements Dan’s answer with a reference to the Microsoft Developer Network (MSDN) documentation. This documentation details the File::Open method.
Specifically, the last argument, 'None', within the File::Open method prevents file sharing with other threads or processes. The MSDN documentation also outlines options for locking a file for read-only, write-only, or a combination of both access types.
Further discussion and contributions can be found in the original Stack Exchange thread. Readers are encouraged to participate and share their insights in the comments section.