Skip to content

/errors/powershell/missing-closing-brace-in-statement-block

PowerShell: Missing closing '}' in statement block or type definition.

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
PowerShell
exit code
1
versions
Windows PowerShell 5.1, PowerShell 7.x
os
windows, linux, macos

Missing closing '}' in statement block or type definition. is a parse error: PowerShell reads the whole script before running it, finds a { without a matching }, and runs nothing. The line and column in the message point to the opening brace that is never closed, not to where the } should go. Add the missing brace at the end of that block, and check for an unterminated string before it, since a missing quote hides the real closing brace.

Reproduce it

The PowerShell parser API reports the same errors as running the file, without running it:

powershell
$cases = @(
  "function Test-It {`n  if (`$true) {`n    'x'`n}",
  "Write-Output (1 + 2",
  "`$name = 'unterminated`n}"
)
foreach ($code in $cases) {
  $errors = $null
  $null = [System.Management.Automation.Language.Parser]::ParseInput($code, [ref]$null, [ref]$errors)
  foreach ($e in $errors) { "line $($e.Extent.StartLineNumber), col $($e.Extent.StartColumnNumber): $($e.Message)" }
}

Output

line 1, col 18: Missing closing '}' in statement block or type definition.
line 1, col 20: Missing closing ')' in expression.
line 1, col 9: The string is missing the terminator: '.

In the first case the if block is closed and the function is not, so the error points to the function's brace on line 1.

Check a script without running it

powershell
$errors = $null
$null = [System.Management.Automation.Language.Parser]::ParseFile("$PWD\script.ps1", [ref]$null, [ref]$errors)
$errors | Format-Table @{n='Line';e={$_.Extent.StartLineNumber}}, @{n='Col';e={$_.Extent.StartColumnNumber}}, Message -AutoSize

This is useful in CI to fail fast on syntax errors in every .ps1 file.

Usual culprits

ProblemExampleFix
Missing } after editingif ($x) { ... (no })add } at the end of the block
Unterminated stringWrite-Host 'it's done'double the quote: 'it''s done', or use double quotes
Indented here-string terminator '@ (with spaces before it)'@ must be at the start of the line

Sources