none
Major Bug/Feature in Powershell 5.0 RRS feed

  • General discussion

  • Hi Guys

    I just stumbled upon a rather irritating error in Powershell 5.0. Apparently you cannot open a file and write to it afterwards using pipes. 

    Compare:

    Powershell 4.0: (Get-Content $file) | Set-Content $file

    Powershell 5.0: (Get-Content $file) | Set-Content $file Error: The process cannot access the file $file because it is being used by another process.

    With PS 4.0 you could used the brackets around Get-Content to close the file handle. With PS 5.0 apparently this doesn't work anymore.

    Any suggestions?

    Best regards

    Michael

    Monday, January 23, 2017 12:13 PM

All replies

  • Suggestions about what?

    You can work around this by writing to a temporary file and then overwriting the original.


    -- Bill Stewart [Bill_Stewart]

    Monday, January 23, 2017 3:22 PM
    Moderator
  • Hi Michael,

    can't reproduce it on an up-to-date Windows 10 machine. Works just fine for me.
    Have you checked whether the file is actually opened in another process?

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Monday, January 23, 2017 3:37 PM
  • I also cannot duplicate the error on a Windows 10 computer with PS version 5.1. I even checked that the file date got updated.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Monday, January 23, 2017 3:47 PM
    Moderator
  • What I found out in the meantime is that it works if you open a new PS session. However in the same session it does only work the first time.

    You can also workaround it by using Out-File instead of Set-Content.

    Monday, January 23, 2017 4:01 PM
  • Works fine with PS 5 on W10, WS2008R2 and WIn7.


    \_(ツ)_/

    Monday, January 23, 2017 4:16 PM
  • I used PS 5 on Win10, 1607 and I could reproduce it every time, strange.

    Maybe Layer 8, had to get up early... :)

    Monday, January 23, 2017 4:23 PM