Skip to content

Instantly share code, notes, and snippets.

@rweijnen
Created March 27, 2023 14:11
Show Gist options
  • Select an option

  • Save rweijnen/11188253b80167a61354376d91cecafa to your computer and use it in GitHub Desktop.

Select an option

Save rweijnen/11188253b80167a61354376d91cecafa to your computer and use it in GitHub Desktop.
PowerShell example how you can keep a file in use, denying read/write access to other processes
# create test.txt in temp folder
$filename = Join-Path -Path $env:temp -ChildPath 'test.txt'
# make sure the file exists
"" | Out-File -LiteralPath $filename
# Open the file with Share options only allowing read...
$f = [System.IO.File]::Open($filename, 'Open', 'Read', 'Read')
"File $fileName is in use and locked"
Write-Host 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Write-Host "Closing $filename..."
$f.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment