且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell 中的 Out-File -append 不会产生新行并将字符串分解为字符

更新时间:2023-11-18 21:30:04

Out-File 默认为 unicode 编码,这就是您看到的行为的原因.使用 -Encoding Ascii 来改变这种行为.在你的情况

Out-File defaults to unicode encoding which is why you are seeing the behavior you are. Use -Encoding Ascii to change this behavior. In your case

Out-File -Encoding Ascii -append textfile.txt. 

Add-Content 使用 Ascii 并默认附加.

Add-Content uses Ascii and also appends by default.

"This is a test" | Add-Content textfile.txt.

至于缺少换行符:您没有发送换行符,因此它不会向文件写入一个.

As for the lack of newline: You did not send a newline so it will not write one to file.