Using gh api in powershell to get a file with a UTF-8 bom adds a corrupted version of the BOM to the begining of the file

Viewed 11

If I execute the following commands from Powershell core 7.2.6 on windows 11 to grab an XML file with a UTF-8 BOM:

gh api -H "Accept: application/vnd.github.raw+text" repos/zippy1981/CodeFirstBetter/contents/Zippysoft.CodeFirst.AD.Importer/Zippysoft_CodeFirst_AD_Importer.csproj | Out-File Zippysoft_CodeFirst_AD_Importer.csproj

I get a file Visual Studio Code thinks has no BOM, but does have 4 characters that seem to be a corrupt version of the BOM

enter image description here

dotnet cannot parse the file until I delete them. Changing the end of the pipeline to Out-File Zippysoft_CodeFirst_AD_Importer.csproj -Encoding utf8BOM does not fix the problem but oes prepend an ACTUAL BOM to it. How do I get rid of those characters?

1 Answers

The best I can do is two long lines of PowerShell to pull the files metadata and contents in base64 and then decode it.

$contents = gh api -H "Accept: application/vnd.github.text" repos/zippy1981/CodeFirstBetter/contents/Zippysoft.CodeFirst.AD.Importer/Zippysoft_CodeFirst_AD_Importer.csproj | ConvertFrom-Json
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($contents.content)) | Out-File -FilePath $contents.name

This seems like a lot of work when the gh api cmd could simply had a flag to diretly write standard output to a file and skip any weirdness of the powershell (or other shell) pipeline.

Related