PowerShell: unexpected token or missing delimiter
Documentation: Microsoft Learn
Find mismatched quotes, brackets and incomplete expressions when a PowerShell script fails to parse.
What it means
The script never reached execution. PowerShell was still turning text into a syntax tree when it hit something it could not place. Nothing in the file ran, including the lines above the one it reported.
Likely causes
- An unbalanced quote, brace, parenthesis, or bracket somewhere earlier in the file
- A typographic quote character pasted in from a document or a web page
- A backtick escape at the end of a line followed by trailing whitespace
- A here-string terminator that does not sit at the start of its own line
- An argument that begins with a hyphen and is therefore read as a parameter name
Check
$tokens = $null
$parseErrors = $null
[System.Management.Automation.Language.Parser]::ParseFile(
(Join-Path $PWD 'Example.ps1'), [ref]$tokens, [ref]$parseErrors
) | Out-Null
$parseErrors | Select-Object Message, ExtentReplace Example.ps1 with the file you want to parse. This command does not execute that script.
Run these checks before changing anything.
What these checks establish
Parse errors contain a location and message. The reported position may follow the missing quote or delimiter. Parsing a file with Parser.ParseFile inspects syntax without executing its commands.
Fix
Use the parser errors below to locate the first broken construct. Check paired quotes, braces, parentheses and here-string delimiters, then parse again. A single unmatched delimiter can cause several later errors. Once syntax is valid, test behavior separately; a parseable script can still be wrong.
Verify: Confirm the parser returns no errors, then run controlled functional tests with known input.