Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a .txt file in which I want to replace the string aaa with bbb.

I have tried the following PowerShell code:

Get-Content c:\1.txt | ForEach-Object { $_ -replace "aaa", "bbb" } | Set-Content c:\1.txt

I get an error that 1.txt is being used by another process. What am I doing wrong?

share|improve this question
up vote 7 down vote accepted
(get-content c:\1.txt) | foreach-object {$_ -replace "prod", "qa1"} | set-content c:\1.txt

The parentheses around Get-Content ensure that the Get operation is completed before the Set operation begins, without this the two functions would both try to access the file at the same time.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .