Home > Mobile >  What is the meaning of "@" in relation to PowerShell and HTML code as a variable?
What is the meaning of "@" in relation to PowerShell and HTML code as a variable?

Time:11-23

I saw a code snippet here some time ago that dealt with PowerShell and HTML. An HTML code was passed into a variable. The beginning and the end were delimited with @" and "@. In a response, the @ delimiter was labelled with a specific term. Unfortunately, I did not save the post. Can someone tell me what this '@' delimitation is called and how exactly it is used?

CodePudding user response:

See this article for more info on here strings which are what that is called. You can use a here string in that manner to define some html.

$html = @"
<body>
    <h1>Header 1</h1>
    <p>Paragraph</p>
</body>
@"

If you didn't use the here string you could run into errors parsing the html in various libraries because of the / that close tags.

CodePudding user response:

The syntax element you describe is a here-string expression:

@"
your string content goes here
...

and can span multiple lines!
"@

Like any other double-quoted string it's expandable (that is, you can embed variables and subexpressions):

@"
1   2 = $(1   2)!

... and the Process ID of this host application is ${PID}
"@

They can also be useful for single-line strings containing mixed verbatim quotation marks:

# no need to escape quotation marks
@"
The movie was titled "Jennifer's body"
"@

as opposed to escaping the quotation marks inline, which can become quite unreadable:

"The movie was titled `"Jennifer's body`""
"The movie was titled ""Jennifer's body"""  # this many double-quotes makes me dizzy
'The movie was titled "Jennifer''s body"'

Here-strings are documented in the about_Quoting_Rules help topic

  • Related