防止在 PowerShell Out-File 命令中使用尾随换行符 [英] Prevent trailing newline in PowerShell Out-File command

查看:46
本文介绍了防止在 PowerShell Out-File 命令中使用尾随换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止 PowerShell 的 Out-File 在输出文本后附加换行符的命令?

How do I prevent PowerShell's Out-File command from appending a newline after the text it outputs?

例如,运行以下命令会生成一个内容为TestTest\r\n"而不仅仅是TestTest"的文件.

For example, running the following command produces a file with contents "TestTest\r\n" rather than just "TestTest".

"TestTest" | Out-File -encoding ascii test.txt

推荐答案

在 PowerShell 5.0+ 中,您将使用:

In PowerShell 5.0+, you would use:

"TestTest" | Out-File -encoding ascii test.txt -NoNewline

但在早期版本中,您根本无法使用该 cmdlet.

But in earlier versions you simply can't with that cmdlet.

试试这个:

[System.IO.File]::WriteAllText($FilePath,"TestTest",[System.Text.Encoding]::ASCII)

这篇关于防止在 PowerShell Out-File 命令中使用尾随换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆