EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell here string
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

PowerShell here string

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.

Syntax

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more