Powershell: It should be possible to disable writing BOMs in new files created with `Out-File` and `>`

Created on 4 Jan 2019  路  5Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

It should be possible to configure PowerShell to not write a BOM when writing files with redirection, such as with echo foo > myfile.txt.

Proposed technical implementation details (optional)

I suggest adding a -NoBOM parameter to Out-File, which disables writing the BOM.

Then I suppose it could be configured as the default like so:

$PSDefaultParameterValues['Out-File:NoBOM'] = $true
Issue-Question Resolution-Answered

All 5 comments

Strictly speaking this is the purview of the encoding utilised; you can specify either -Encoding UTF8 or -Encoding UTF8BOM with Out-File in PS Core to determine whether you'd like the byte order mark or not. 馃槃

@vexx32 It writes the BOM even when "UTF8" is specified as the encoding:

PS C:\Users\radix> out-file MyCoolFile.txt -Encoding UTF8 -InputObject "hello, world!"
PS C:\Users\radix> python -c "print repr(open('MyCoolFile.txt').read())"
'\xef\xbb\xbfhello, world!\n'

Which version of PS are you using? It behaves correctly in PS Core 6.1.0, at least. 馃檪

PSVersionTable:

Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Comparison of file bytes:

PS C:\Program Files\PowerShell\6> "test" | out-file -Encoding utf8 -path c:\ecp\test.txt
PS C:\Program Files\PowerShell\6> @([system.io.file]::ReadAllBytes('c:\ecp\test.txt'))
116
101
115
116
13
10
PS C:\Program Files\PowerShell\6> "test" | out-file -Encoding utf8bom -path c:\ecp\test.txt
PS C:\Program Files\PowerShell\6> @([system.io.file]::ReadAllBytes('c:\ecp\test.txt'))
239
187
191
116
101
115
116
13
10

I guess I'm on 5.1 :( I'm using the version included in my Windows 10.

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17134  407

Thanks for confirming this works now. I guess I'll have to figure out how to upgrade!

You can download the MSI installer from here for either the latest preview or stable versions here:
https://github.com/PowerShell/PowerShell/releases

PS Core installs side-by-side with Windows PowerShell. 馃檪

Was this page helpful?
0 / 5 - 0 ratings