|
Weirdness with get-content | replace | set-content - file content is deleted!! This has now happened 6 or 7 times with the same file.
As part of something else that I was doing I wanted to do a replace
operation on some text in a text file.
Here is the text copied from the screen.
PS C:\PowerShellScripts> get-content NewUsers.txt
newuser,NAM\aw789,New Registered Users/
fullname,NAM\aw789,"Smith, John [CIR]"
setrole,NAM\aw789,User
newuser,NAM\sm902,New Registered Users/
fullname,NAM\sm902,"Adams, Penny [SOB]"
setrole,NAM\sm902,User
newuser,NAM\wb008,New Registered Users/
fullname,NAM\wb008,"Algar, Camilla [GM]"
setrole,NAM\wb008,User
PS C:\PowerShellScripts> get-content NewUsers.txt | foreach-object {$_
-replace "NAM\\", "ABC\"} | set-content NewUsers.txt
Set-Content : The process cannot access the file
'C:\PowerShellScripts\NewUsers
..txt' because it is being used by another process.
At line:1 char:86
+ get-content NewUsers.txt | foreach-object {$_ -replace "NAM\\",
"ABC\"} | set
-content <<<< NewUsers.txt
PS C:\PowerShellScripts> get-content NewUsers.txt | foreach-object {$_
-replace "NAM\\", "ABC\"} | set-content NewUsers.txt
PS C:\PowerShellScripts> get-content NewUsers.txt
PS C:\PowerShellScripts>
Notice the content of NewUsers.txt at the beginning. And at the end
it's gone.
The first time I try the replace I consistently get an error about the
file being in use.
The second time I run it, then check the content of the file it's been
wiped.
It's either a bug or something that I'm doing wrong. I can't see that
anything that I am doing would either lock the source file, nor wipe
it. Am I missing some "Doh!" content in my code?
The whole content of the source file is shown. If someone could try to
repro this I would be grateful.
I've tried it with another source file. Same result.
Thanks.
Andrew Watt MVP |