lipkau / PsIni

Work with INI files in PowerShell using hashtables
http://lipkau.github.io/PsIni
MIT License
151 stars 50 forks source link

Out-IniFile: UTF-8 without BOM #68

Open exchange12rocks opened 2 years ago

exchange12rocks commented 2 years ago

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.

lipkau commented 2 years ago

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.

exchange12rocks commented 2 years ago

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

BlueMeetsWhite commented 2 years ago

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

MyITGuy commented 11 months ago

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

damngamerz commented 4 months ago

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