You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why does Add-Content require a read lock when appending to files? #5924
I (and various customers of my company) notice that Add-Content often fails to append to files with an error like:
Add-Content : The process cannot access the file
'C:\Users\Administrator\logs\access_log.log' because it is being used by another process.
This happens if the file in question is being read ("tailed") by some other process at the same time, e.g. a log collection framework.
I confirmed via ProcMon that this is because Add-Content takes a read lock on its target file. So if the file is already open for read by another process (even if that process has taken no locks), the cmdlet will fail.
Could FileShare.Write be changed to FileShare.ReadWrite?
This applies to both Windows PowerShell and PowerShell Core
Reacted by FireInWinter, TigerBoom, Aftab Hussain, Niels Swimberghe, Daniele Catanesi, Zhou Fang, sbernardjr, Muhammad Ihsan, Chris Trotter, RobbertJanSW and 10 more
Activity
yangl900 commentedon Aug 9, 2018
+1 We see the same issue.
fonta commentedon Sep 22, 2018
One more
mbwmbw1337 commentedon Oct 18, 2018
Agreed. Very annoying. This needs to be fixed.
cpumanaz commentedon Oct 19, 2018
Could really use the ability to tail logs without having to use workarounds.
Here is a nice work around [string]$myString >> $myLogFile
SteveL-MSFT commentedon Oct 20, 2018
I don't see any inherent problem with changing it to FileShare.ReadWrite. I'll submit a PR.