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 Multiline 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

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Multiline String

PowerShell Multiline String

Introduction to PowerShell Multiline String

A string is nothing, but a set of character enclosed within “. A multiline string is a string whose value exceeds beyond one line. In that, the string should be enclosed within a here-string(@””@). Newline or carriage return value can also be used to create a multiline string. Multiline string can also be defined by using a line break within double-quotes. This article will explain in detail about multi-line string, its syntax uses along with appropriate examples.

Syntax:

Here-string is used for defining multiline string as follows. The first and last line shouldn’t contain anything.

@”
First-line
Second line
Third line
“@

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

When we don’t the variable or expression to be not evaluated then the string value can be enclosed in single quotes.

Example:

Write-Host "example of multiline string using here string"
$test=@"
First-line
second line
"@
Write-Host $test
Write-Host "example using literal string"
$test1=@'
first line
$(Get-Date)
second line
'@
Write-Host $test1

Output:

powershell 1

Even without here-string multi-line string can be defined as below

Input:

Write-Host "example of multiline without here string"
$text = {
Myself vignesh
am from chennai
am a freelancer
this is fourth line
}
write-Host $text.ToString()

Output:

powershell multiline string 2

powershell multiline string 2-1

Working of Multiline String using her String

PowerShell understands a string as multiline when it sees a @” succeeded by a newline. It ends when it ends with a “@. The main purpose of this is that it allows users to create strings with quotes, symbols, new lines, or other formatting texts. Whenever an error is thrown with reference to multiline string it is wise to first check the first line and there is no space between @ and “. Like normal string variable multiline string can also be literal or expanding. Single quotes denote literal and double quotes represent expanding. When a value or an expression is used inside a single-quoted multi-string it is printed as a literal string, whereas if a variable or expression is used inside a double-quoted multi-string it is evaluated and the actual value is printed rather than the literal value. Four possible combinations of multi-line strings can be created based on the needs of the user. In the below examples the various scenarios of multi-line string are discussed in detail.

Examples

Let us discuss examples of PowerShell Multiline String.

Example #1

Write-Host "Converting multine line string to a single line" -ForegroundColor Green
Write-Host "Get the contents of the file before converting" -ForegroundColor Green
Write-Host "Displaying the contents of the file" -ForegroundColor Green
$input = Get-Content -Path "C:\vignesh\multiline.txt"
foreach($line in $input)
{
Write-Host $line
}
Write-Host "Merging the contents of the file into a single line" -ForegroundColor Green
foreach ($line in $input)
{
$op = $op + $line
}
$op| Out-File C:\vignesh\multiline.txt -Force
Write-Host "After merging" -ForegroundColor Green
$final = Get-Content -Path "C:\vignesh\multiline.txt"
Write-Host $final

Output:

example 1

Example #2

Write-Host "Demo of splitting multiline string into object" -ForegroundColor Green
Write-Host " the multiline string is as follows" -ForegroundColor Green
$ip = @"
Student Name: Vignesh Student Id: 1 Age: 28
Student Name: Nandhini Student Id: 2 Age: 29
Student Name: Vyapini Student Id: 3 Age: 30
"@
Write-Host "Before splitting into object" -ForegroundColor Green
Write-Host $ip
Write-Host "After splitting the multiline string into object" -ForegroundColor Green
$ip -split "`n" |
Select-String 'Student Name: (\w+) Student Id: (\w+) Age: (.+)' |
ForEach-Object {
New-Object PSObject -Property ([Ordered] @{
"Student Name" = $_.Matches[0].Groups[1].Value
"Student Id"= $_.Matches[0].Groups[2].Value
"Age" = $_.Matches[0].Groups[3].Value
})
}

Output:

example 2

Example #3

Write-Host "Demo of different types of multi-string" -ForegroundColor Green
Write-Host "Simple example of multi-string enclosed in double quotes" -ForegroundColor Green
$ip = "
This is first line.
This is second line.
This is third line
"
Write-Host $ip
Write-Host "Simple example of multi-string in single quotes" -ForegroundColor Green
$ip = '
This is first line.
This is second line.
This is third line
'
Write-Host $ip
Write-Host "Example of here string in double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is second line.
This is third line
"@
Write-Host $ip
Write-Host "Example of here string in single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is second line.
This is third line
'@
Write-Host $ip
Write-Host "using value in double quotes" -ForegroundColor Green
$date=Get-Date
$ip=@"
Todays date is below
$date
"@
Write-Host $ip
Write-Host "using value in single quotes" -ForegroundColor Green
$date=Get-Date
$ip=@'
Todays date is below
$date
'@
Write-Host $ip
Write-Host "in single quotes the string literal is printed and not the value" -ForegroundColor Green
Write-Host "Using double quotes inside a double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is "second" line.
This is third line
"@
Write-Host $ip
Write-Host "Using single quotes inside a double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is 'second' line.
This is third line
"@
Write-Host $ip
Write-Host "Using single quotes inside a single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is 'second' line.
This is third line
'@
Write-Host $ip
Write-Host "Using double quotes inside a single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is "second" line.
This is third line
'@
Write-Host $ip

Output:

example 3

Conclusion

Thus, the article covered in detail multiline string in PowerShell. It also explained in detail how multiline string can be created using various methods such as here-string, single quotes, and double quotes methods and their differences. It also demonstrated various example programs that walked us through how to create multiline strings and work with them. To learn more in detail it is advisable to write sample scripts and practice them.

Recommended Articles

This is a guide to PowerShell Multiline String. Here we discuss the Introduction, syntax, Working of multiline string using her string, and examples. You may also have a look at the following articles to learn more –

  1. PowerShell where
  2. PowerShell uninstall module
  3. PowerShell Match
  4. Start PowerShell from cmd
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
0 Shares
Share
Tweet
Share
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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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