如何为 powershell tee 命令指定文件格式?
How to specify file format for powershell tee command?
使用 cmd.exe,我 运行 powershell ".\t.bat 2>&1 | tee results.txt"
在 Windows 10 笔记本电脑上,生成的文件 results.txt
出现在“UCS-2 LE根据 Notepad++ 的 BOM" 格式。我的 VBScript 和其他人无法阅读。
是的,我可以使用“编码 --> 以 ASCII 编码”之类的方法,但这是一个手动干预,我希望自动化该过程。
是否有一些 PowerShell 选项可以让我说出 tee 输出文件应该是什么格式?
或者一些 PowerShell/DOS 可以为我“修复”/更改文件格式的命令?
谢谢。
改用Out-File。
Copy-pasted 直接来自文档
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/tee-object?view=powershell-7
Notes
You can also use the Out-File cmdlet or the redirection operator, both
of which save the output in a file but do not send it down the
pipeline.
Beginning in PowerShell 6, Tee-Object uses BOM-less UTF-8 encoding
when it writes to files. If you need a different encoding, use the
Out-File cmdlet with the Encoding parameter.
不幸的是,Tee-Object
cmdlet(在 Windows 上的 built-in 别名是 tee
[1])没有从 PowerShell 7.1 开始没有 -Encoding
参数 - 与其他写入文件的 cmdlet 不同。
Tee-Object
在 Windows PowerShell 中的默认编码是“Unicode”(UTF-16LE)(严格来说是 not 与 UCS-2 LE[2]) 相同,而在 PowerShell [Core] v6+ 中,所有cmdlets 使用相同的编码,它是 BOM-less UTF-8.
它没有正式记录,但您可以间接控制Tee-Object
的输出编码,方法是将编码分配给$PSDefaultParameterValues['Out-File:Encoding']
,但请注意这样做还会更改 Out-File
的默认字符编码,从而也会更改 >
/ >>
.
以下命令使 Tee-Object
使用带有 BOM 的 UTF-8 编码:
powershell -c "$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'; .\t.bat 2>&1 | tee results.txt"
在两个 PowerShell 版本中,您可以传递给 Out-File -Encoding
的任何值都可以分配给 $PSDefaultParameterValues['Out-File:Encoding']
,这在 Windows PowerShell 中是仅限于预定义的枚举值,而在 PowerShell [Core] v6+(其可执行文件是 pwsh
而不是 powershell.exe
)中,您还可以传递任何派生自类型 System.Text.Encoding
.
[1] 这个别名 不是 在 Unix-like 平台上定义的,以免影响本地 tee
实用程序.
[2] UCS-2 是一种过时的 固定 宽度编码,其中每个字符正好占用 2 个字节。 UTF-16 是该编码的 _variable_width 超集 ,它还能够通过 对 [= 对 BMP(基本多语言平面)之外的 Unicode 字符进行编码two-byte 个序列中的 63=] 个称为 代理对 。
使用 cmd.exe,我 运行 powershell ".\t.bat 2>&1 | tee results.txt"
在 Windows 10 笔记本电脑上,生成的文件 results.txt
出现在“UCS-2 LE根据 Notepad++ 的 BOM" 格式。我的 VBScript 和其他人无法阅读。
是的,我可以使用“编码 --> 以 ASCII 编码”之类的方法,但这是一个手动干预,我希望自动化该过程。
是否有一些 PowerShell 选项可以让我说出 tee 输出文件应该是什么格式? 或者一些 PowerShell/DOS 可以为我“修复”/更改文件格式的命令?
谢谢。
改用Out-File。
Copy-pasted 直接来自文档 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/tee-object?view=powershell-7
Notes
You can also use the Out-File cmdlet or the redirection operator, both of which save the output in a file but do not send it down the pipeline.
Beginning in PowerShell 6, Tee-Object uses BOM-less UTF-8 encoding when it writes to files. If you need a different encoding, use the Out-File cmdlet with the Encoding parameter.
不幸的是,Tee-Object
cmdlet(在 Windows 上的 built-in 别名是 tee
[1])没有从 PowerShell 7.1 开始没有 -Encoding
参数 - 与其他写入文件的 cmdlet 不同。
Tee-Object
在 Windows PowerShell 中的默认编码是“Unicode”(UTF-16LE)(严格来说是 not 与 UCS-2 LE[2]) 相同,而在 PowerShell [Core] v6+ 中,所有cmdlets 使用相同的编码,它是 BOM-less UTF-8.
它没有正式记录,但您可以间接控制Tee-Object
的输出编码,方法是将编码分配给$PSDefaultParameterValues['Out-File:Encoding']
,但请注意这样做还会更改 Out-File
的默认字符编码,从而也会更改 >
/ >>
.
以下命令使 Tee-Object
使用带有 BOM 的 UTF-8 编码:
powershell -c "$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'; .\t.bat 2>&1 | tee results.txt"
在两个 PowerShell 版本中,您可以传递给 Out-File -Encoding
的任何值都可以分配给 $PSDefaultParameterValues['Out-File:Encoding']
,这在 Windows PowerShell 中是仅限于预定义的枚举值,而在 PowerShell [Core] v6+(其可执行文件是 pwsh
而不是 powershell.exe
)中,您还可以传递任何派生自类型 System.Text.Encoding
.
[1] 这个别名 不是 在 Unix-like 平台上定义的,以免影响本地 tee
实用程序.
[2] UCS-2 是一种过时的 固定 宽度编码,其中每个字符正好占用 2 个字节。 UTF-16 是该编码的 _variable_width 超集 ,它还能够通过 对 [= 对 BMP(基本多语言平面)之外的 Unicode 字符进行编码two-byte 个序列中的 63=] 个称为 代理对 。