Updated March 10, 2023
Introduction to PowerShell here string
PowerShell Here-String is the method of inserting the multiple lines of the strings or the commands inside the enclosure, also known as the string array, and the best way of representing the data with the free text and string block is represented by the @” “@ with either single-quote or the Double-quote inside which can execute text, expressions, sub-expressions, and create a different format file.
Syntax
PowerShell here-String is represented with the enclosure @” “@, and some people also refer it to the PowerShell String array.
How does the PowerShell “Here String” works?
The double quotes represent Powershell here-string@” ”@ or the single quote @’ ’@, but both are not the same; they have significant differences when it comes to the expansion of the variable or the expressions.
First, let’s simply try our multiple-line string enclosure to check how it works.
@"
This is a string block with multiple lines.
This is the second line
This is the third line
"@
Output:
We have mentioned earlier; this is free text, so you can put string anywhere inside the block, and it will display as it mentioned.
@"
This is the first line
This is the second line
This is the third line
"@
Output:
When you declare the here-string block @” “@, white space is a very important factor for it. You can provide the White-space at the start of syntax @” but can’t provide the white space at the end of the syntax “@ it is considered as invalid. See the example below,
@"
This is the first line
This is the second line
This is the third line
"@
The end block “@ contains the white space, and the output will generate an error.
Here-String with the single quote is similar to the double quote enclosure when writing a text. We can also write the above string input as shown below.
@'
This is the first line
This is the second line
This is the third line
'@
We can also store the here-string into the variable.
$str = @"
This is the first line
This is the second line
This is the third line
"@
$str
The significant difference that comes between the single quote and the double-quote here-string is when we use the variable inside them. Single quote here-string can’t expand the name of the variable.
$val = 10
@"
This is a string
The value is : $val
"@
Output:
When using a single quote enclosure.
$val = 10
@'
This is a string
The value is : $val
'@
Output:
You can see that the single-quote enclosure can’t expand the variable name. Similarly, when expanding the variable single-quote here-string command fails to expand it, as shown below.
@'
This is a string
Today's date is : $(Get-Date)
Total: $(4 + 5 + 6)
'@
Output:
When we use the double-quote here-string.
@"
This is a string
Today's date is : $(Get-Date)
Total: $(4 + 5 + 6)
"@
Output:
The here-String method is not limited to displaying the multiple lines of the string or the expansion of the variable or the expressions, but we can also create CSV, JSON, HTML, etc. files with their data and then convert them to the respective file format and they are shown in the examples below.
Examples of PowerShell here string
Given below are the example of PowerShell here string:
Example #1 – Using Here-String to create a CSV file.
To create a CSV file format along with the data, we can use the Here-string method, as shown below.
$csv = @"
Name, EmpID, City, Role
Jack, 1001, Atlanta, Permanent
Thomas, 1002, Sweden, Contractor
Lisa, 1003, London, Permanent
"@
$csv | ConvertFrom-Csv
Output:
To store the output in CSV, you can use the below command.
$csv | ConvertFrom-Csv | Export-CSV C:\temp\empdata.csv -NoTypeInformation
Example #2 – Using the Here-String command to create a JSON file.
To create a JSON file with the Here-String.
@"
{
"Type": "Fruits",
"Fruits": [
"Apple",
"Grapes",
"Mango"
],
"Country": [
"India",
"US"
]
}
"@ | ConvertFrom-Json
Output:
Example #3 – Creating a Hashtable with Here-String.
Another type of data we can convert is the Hashtable. To create a Hashtable with the Here-String command, use the below method.
$hash = @"
Name = Jackson
Employer = BigData
EmpID = 2032
Type = Permanent
"@
$hash | ConvertFrom-StringData
Output:
Example #4- Using here-string to create an HTML file.
We can create an HTML file using the PowerShell here-string command, as shown below.
$html = @'
<!DOCTYPE html>
<html>
<head>
<title> PowerShell HTML Page </title>
</head>
<body>
<h1> This is a Heading </h1>
<p> This is a paragraph </p>
</body>
</html>
'@
$html | Out-File C:\Temp\testhtml.html
Output:
Example #5 – Using Here-String to create a new PowerShell function.
The Here-String command is so beneficial that you can even create a PowerShell function and store it in the new file. As shown below.
@'
function PrintName{
param(
[Parameter(Mandatory=$true)]
[String]$Name
)
Write-Output "Here-String.. Print Name: $Name"
}
'@ | Out-File C:\Temp\PrintName.ps1
We are using the single-quoted here-string because we don’t want to print the $Name variable, but we need to store it into the file. If you directly store the file without function, you can pass parameters directly from the command line, as shown below.
@'
param(
[Parameter(Mandatory=$true)]
[String]$Name
)
Write-Output "Here-String.. Print Name: $Name"
'@ | Out-File C:\Temp\PrintName.ps1
Output:
There are so many examples that you can almost create any kind of file, functions, expressions with the here-string command, and this nature makes the here-string more powerful. Not only in PowerShell, but other programming languages use the here-string command for its flexible nature.
Conclusion
PowerShell here-string is one of the most effective methods to write the multiple lines of the string, including expansion of variables, expressions, sub-expressions inside the enclosure. We can also use the here-string to create a structure of the CSV, JSON, HTML, etc., and letter converts them to their respective formats.
Recommended Articles
This is a guide to PowerShell here string. Here we discuss How does the PowerShell “Here String” works along with the examples and outputs. You may also have a look at the following articles to learn more –