Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Code to put an exclusive lock on a file
param([string]$fileName)
#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
$file.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment