PsIni icon indicating copy to clipboard operation
PsIni copied to clipboard

Out-IniFile: UTF-8 without BOM

Open exchange12rocks opened this issue 4 years ago • 5 comments

Hi Oliver! Thank you very much for your module - I love it - it is a great help to me!

I use PsIni in my project PSGPPreferences where I update GPT.INI in Group Policies with it. Unforunately recently I stumbled upon this bug: https://github.com/exchange12rocks/PSGPPreferences/issues/28 I need to be able to write those ini-files in UTF-8, but without BOM, in a single transaction, i.e. I cannot remove BOM afterwards.

Currently Out-IniFile uses Out-File, which always writes BOM in Windows PowerShell, unless the user disables it globally. TBH I'd rather do not introduce global changes on my users' PCs. Is there a particular reason Out-IniFile uses Out-File? Would it be possible to switch to Set-Content? As far as I am aware it supports UTF-8 w/o BOM.

exchange12rocks avatar Apr 02 '22 08:04 exchange12rocks

There was a reason for using Out-File over Set-Content. But I don't remember what it was.

But neither of the two commands support UTF8NoBom on Windows Powershell v5.

What can be done, is to dynamically extend the allowed values for parameter depending on the powershell version.

But I haven't had much time for working on the module anyway. And I would first finish the new CICD and the new major version before looking into this.

lipkau avatar Apr 02 '22 11:04 lipkau

The UTF8 encoding, which Set-Content uses, is w/o BOM in Windows PowerShell v5 - just checked

exchange12rocks avatar Apr 02 '22 13:04 exchange12rocks

Just getting to grips with PowerShell and your excellent script. I noted something which may be related to this issue. It doesn't cause me any functional issues, just cosmetics.

The INI file I am processing is German, so it has the German umlaut characters within the comments in the INI

Here is an example:

;## ausführliche Variante

Becomes

;## ausf�hrliche Variante

BlueMeetsWhite avatar Apr 10 '22 08:04 BlueMeetsWhite

In my environment (Windows), the Out-IniFile function does encode using the UTF8 parameter, but the resulting file encoding is UTF8 with BOM.

To get UTF8 (no BOM), the Out-IniFile and its internal Out-Keys functions need to be updated to support the "Default" encoding.

This was the parameter change I made (to both functions) to get the resulting file encoded correctly.

[ValidateSet("Default", "Unicode", "UTF7", "UTF8", "ASCII", "BigEndianUnicode", "Byte", "String")] [Parameter( Mandatory )] [string] $Encoding = "Default",

Cheers

MyITGuy avatar Sep 28 '23 20:09 MyITGuy

I also faced the same problem. For me the easiest workaround was to do something like this.

$ini | Out-IniFile -FilePath 'PATH_TO_FILE' -encoding ascii

damngamerz avatar Apr 17 '24 07:04 damngamerz