How to lock a file in Windows without changing it or using third party tools?

I think PowerShell is probably the neatest way to accomplish this. Something like the following:

#Specify the file name
$fileName = "C:\myfile.txt"

#Open the file in read only mode, without sharing (I.e., locked as requested)
$file = [System.io.File]::Open($fileName, 'Open', 'Read', 'None')

#Wait in the above (file locked) state until the user presses a key
Write-Host "Press any key to continue ..."
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

#Close the file (This releases the current handle and unlocks the file)
$file.Close()

While paused, the above script causes the following prompt when attempting to open up "myfile.txt":

enter image description here


Since I don't like installing software that I won't actually use, I found this jewel of a VBScript:

Const ForAppending = 8
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set f = oFSO.OpenTextFile("C:\Tst\My2.txt", ForAppending, True)

MsgBox "Press OK to unlock the file(s)"

All credit goes to Torgeir Bakken, you can see the full post here: http://www.pcreview.co.uk/threads/how-to-lock-files.1518829/