I'm trying to create a text file that contains the content of my source code files concatenated. I've came up with the following concise code:
Get-ChildItem -Recurse MySourcePath -Include "*.htm", "*.cs" | Foreach-Object
{Get-Content $_.FullName | Add-Content MyFile.txt}
The problem is, after multiple files (hundreds or thousands), I'm starting to get the following error:
Add-Content : The process cannot access the file 'MyFile.txt' because it is being used by another process.
It seems like concurrency issue and I thought it was related to the pipelining mechanism, but using foreach
didn't solve the problem.
What is the right way in PowerShell to protect my file from multiple writes? Is there a way to still utilize the pipeline?