Skip to content

PowerShell fails to parse .ps1 files when using UTF‑8 BOM + Chinese filename + OneDrive path (parser reports missing braces) #26642

@spinlocustchan-sys

Description

@spinlocustchan-sys

Prerequisites

Steps to reproduce

After discuss with Copilot, Copilot strongly suggest to report the following issue.
The following contents and wordings are all create by Copilot, not by me. I'm just a beginner on coding, can not understand most of it.

Summary
When a PowerShell script (.ps1) is saved with UTF‑8 with BOM, stored under a OneDrive path, and the filename contains Chinese characters, PowerShell fails to parse the script and throws errors such as:

MissingEndCurlyBrace

Try statement is missing catch or finally block

Unexpected token 'finally'

Missing '}' in statement block

The same script works perfectly when:

The file is renamed to an English filename, or

The file is saved as UTF‑8 (without BOM), or

The file is moved outside OneDrive, or

The script is run inside VS Code (VS Code analyzer does not detect any syntax errors)

This indicates a parser bug triggered by the combination of:

UTF‑8 BOM

OneDrive path

Chinese filename

PowerShell parser behavior

Environment
Windows 11

PowerShell 7.x

VS Code latest version

Script stored under:
C:\Users<user>\OneDrive\VS Code\中文檔名.ps1

Script encoding: UTF‑8 with BOM (VS Code default for Chinese filenames)

Steps to Reproduce
Create a PowerShell script with valid syntax, for example:

powershell
try {
Write-Host "Hello"
} catch {
Write-Host "Error"
}
Save the file as:
測試腳本.ps1

Save the file under a OneDrive folder:
C:\Users<user>\OneDrive\VS Code\測試腳本.ps1

Ensure VS Code saves the file as UTF‑8 with BOM

Run the script:

powershell
.\測試腳本.ps1
Expected Behavior
PowerShell should execute the script normally.

Actual Behavior
PowerShell throws parser errors:

程式碼
MissingEndCurlyBrace
Try statement is missing catch or finally block
Unexpected token 'finally'
Even though the script is syntactically correct.

Workarounds
The script works if:

Renamed to English filename

Saved as UTF‑8 (without BOM)

Moved outside OneDrive

Executed inside VS Code terminal

Conclusion
This appears to be a parser bug triggered by BOM + OneDrive + Chinese filename.
It causes PowerShell to misinterpret the script and break the syntax tree.

Please investigate and fix in future PowerShell releases.

Expected behavior

PowerShell should execute the script normally.

Actual behavior

PowerShell throws parser errors:

MissingEndCurlyBrace
Try statement is missing catch or finally block
Unexpected token 'finally'

Even though the script is syntactically correct.

Error details

Workarounds
The script works if:

Renamed to English filename

Saved as UTF‑8 (without BOM)

Moved outside OneDrive

Executed inside VS Code terminal

Conclusion
This appears to be a parser bug triggered by BOM + OneDrive + Chinese filename.
It causes PowerShell to misinterpret the script and break the syntax tree.

Please investigate and fix in future PowerShell releases.

Environment data

Environment
Windows 11

PowerShell 7.x

VS Code latest version

Script stored under:
C:\Users\<user>\OneDrive\VS Code\中文檔名.ps1

Script encoding: UTF‑8 with BOM (VS Code default for Chinese filenames)

Visuals

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.Needs-ReproThe issue author needs to provide repro steps.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions