PowerShell Learning: Here-String (@” “@)

Hi All,
Greetings.
In this article I will discuss one of the PowerShell string concept – Here-String
What is Here-String?
- Here-String are used to declare multi line text.
- Here-String are specifically useful for managing large blocks of text like HTML, XML, JSON or SQL queries
- Here-String is enclosed within
@"and"@delimiters, with the text in between.
Syntax:
- A here-string is enclosed by an at sign (
@) and a single or double quotation mark ('or") - A here-string ends with a newline followed instantly by
"@ or'@ on its own line
Example:
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>>> }>> "@
- In the above example we have declared the variable $test, which includes the HTML text
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:\>

Use cases of Here-String
- Here-String are specifically useful for managing large blocks of text like HTML, XML, JSON or SQL queries
- Here-String are useful to store code snippets of languages (like C# or Python) within a PowerShell script
- Here-String are useful for API calls
- Here-String are useful while using special characters, like single and double quotes. These characters can be used without escaping
- We can use Here-String to store and manipulate the file content
- Here-String can also be used to create CSV file
Thanks for reading ! Stay tuned for more PowerShell concepts !
HAPPY LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂

1 Response
[…] We have detailed article on here-string – PowerShell Learning: Here-String (@” “@) […]