PowerShell Syntax Errors
In some circumstances, you might see one of the following errors (or a similar error) from a worklet, although the PowerShell script does not error when executed from the PowerShell ISE or console session.
The string is missing the terminator: '.
Missing closing '}' in statement block or type definition
Unexpected token in expression or statement
You must provide a value expression following the -like operator
Symptoms
- Activity log shows syntax errors from a PowerShell worklet.
- The same script from the worklet runs with no errors from the PowerShell ISE or console session.
Root Cause
- Unicode characters such as en dash, em dash, left quote and right quote are not handled properly by the PowerShell interpreter.
- These characters are frequently inserted into scripts as a result of copying and pasting source code from a rich text editor or webpage.
Steps to resolution
- Identify any Unicode characters in your script, replacing them with the non-Unicode equivalent.
- Look for cmdlet arguments that are not properly highlighted - indicating the preceding dash is incorrect.
- Look for quoted strings with left and right quotes around them instead of straight quotes.
- Paste the script into a UTF-8 editor or conversion tool and review.