Created
March 27, 2023 14:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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