EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell here string
 

PowerShell here string

Updated March 10, 2023

PowerShell here string

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

PowerShell here string output 1

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:

PowerShell here string output 2

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.

PowerShell here string output 3

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:

PowerShell here string output 4

When using a single quote enclosure.

$val = 10
@'
This is a string
The value is : $val
'@

Output:

PowerShell here string output 5

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:

output 6

When we use the double-quote here-string.

@"
This is a string
Today's date is : $(Get-Date)
Total: $(4 + 5 + 6)
"@

Output:

output 7

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:

output 8

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:

output 9

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:

output 10

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:

output 11

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:

output 12

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 –

  1. PowerShell Wait
  2. PowerShell XML
  3. PowerShell change directory
  4. PowerShell Like Operator

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW