Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out-IniFile: UTF-8 without BOM #68

Open
exchange12rocks opened this issue Apr 2, 2022 · 5 comments
Open

Out-IniFile: UTF-8 without BOM #68

exchange12rocks opened this issue Apr 2, 2022 · 5 comments

Comments

@exchange12rocks
Copy link

exchange12rocks commented Apr 2, 2022

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: exchange12rocks/PSGPPreferences#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
Copy link
Owner

lipkau commented Apr 2, 2022

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
Copy link
Author

exchange12rocks commented Apr 2, 2022

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

@BlueMeetsWhite
Copy link

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
Copy link

MyITGuy commented Sep 28, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants