Skip to content

Use UTF-8 with BOM encoding for powershell/pwsh shell script (ps1) on Windows

Summary

My GitLab pipeline job failed because PowerShell complained that Unexpected token 'xxx' in expression or statement error. After investigating, I found the problem is text encoding. According to Microsoft documentation:

If you need to use non-Ascii characters in your scripts, save them as UTF-8 with BOM. Without the BOM, Windows PowerShell misinterprets your script as being encoded in the legacy "ANSI" codepage.

Unfortunately the generated script custom-executor1915473468\script2965850816\script.ps1 is UTF8noBOM and sometime it contains non-ASCII characters (eg. username). So it should use correct encoding to work.

config.toml
[[runners]]
  executor = "custom"
  shell = "powershell"
  [runners.custom]
    run_exec = "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe"
    run_args = ["-Command", '''chcp 65001; Import-Module "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"; Enter-VsDevShell ed2b4e24 -SkipAutomaticLocation -DevCmdArguments "-arch=x64 -host_arch=x64"; ''']

Relevant logs and/or screenshots

image

Environment description

Windows Server 2025 with PowerShell 5.1.26100.1591 Desktop

Edited by Conor Hyde