PowerShell – resolving error – “No characters are allowed after a here-string header but before the end of the line”

here-string
here-string

Hi All,

Greetings.

Today new issue and solution.

Background

  • I am executing few PowerShell CMDLET in my PowerShell 7 app.
  • I required a string variable storing my bit long string.
  • As I am assigning my string variable I am getting an error

Error : No characters are allowed after a here-string header but before the end of the line

PowerShell command line interface displaying a parser error related to a malformed HTML doctype declaration.
image 1: PowerShell – error while assigning string to variable

Solution

  • I did research and understand the concept of here-string.
  • We have detailed article on here-string – PowerShell Learning: Here-String (@” “@)
  • Then I realized text in here-string always start with new line.
  • I have started the text on the same line.
  • It should be like that

PS C:\> $test = @"
>> {
>> <!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"></html>
>> }
>> "@
PS C:\>

A screenshot of a PowerShell window displaying a script assignment to a variable named 'test', which includes HTML doctype and XML namespace declarations.
image 2: correct way to use here-string

References

Thanks for reading!!! Please feel free to discuss in case any questions / suggestions / thoughts !!!

HAVE A GREAT TIME AHEAD !!! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.