如何防止PowerShell的Out-File命令在输出的文本后附加换行符?
例如,运行以下命令会生成一个内容为"TestTest\r \n"而不仅仅是"TestTest"的文件.
"TestTest" | Out-File -encoding ascii test.txt
Run Code Online (Sandbox Code Playgroud) 我能够找到如何GetInvalidFileNameChars()在PowerShell脚本中使用该方法.但是,它似乎也过滤掉了空白(这是我不想要的).
编辑:也许我不是很清楚地问这个问题.我希望下面的函数包含文件名中已存在的空格.目前,脚本过滤掉空格.
Function Remove-InvalidFileNameChars {
param([Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$Name
)
return [RegEx]::Replace($Name, "[{0}]" -f ([RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())), '')}
Run Code Online (Sandbox Code Playgroud)