#PowerShell set-content cannot access the file

gc file.txt | % {$_ -replace ‘xxx’, ‘yyy’} | sc file.txt
(gc file.txt) | % {$_ -replace ‘xxx’, ‘yyy’} | sc file.txt

These two commands are completly different. The first one will not work with the error message “The process cannot access the file…” When you try to set the content of the file after processing the first line, we still have it open so that we can send you the second line. To tackle with this problem we need to load the entire content of the file into memory before making any changes. The second command does it.


Leave a Reply to Konstantin Vlasenko Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.