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 Escape Character
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 Escape Character

By Priya PedamkarPriya Pedamkar

PowerShell Escape Character

Introduction to PowerShell Escape Character

In PowerShell, escape characters are a sequence of special characters that are not a part of the standard character set. These are used to denote the PowerShell compiler on how to treat the next characters in the sequence. The escape character is PowerShell that usually prefixed by a backquote (`), which means the character must be treated in a literal manner and not in another way around. The backtick character is otherwise known as a grave accent and its ASCII value is 96. Escape sequence must be used within “” to be considered as an escape sequence.

Syntax:

The following is an example of an escape sequence

`t-

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

This is used for horizontal tab spacing.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

Code:

Write-Host "first word" `t "second word"

Output:

PowerShell Escape Character - 1

List of available Escape Sequence in PowerShell

The following are some of the escape sequences available in PowerShell.

Escape Sequence Use
`0 This denotes null character
`a This is for alert
`b This denotes backspace
`e This is for escape sequence
`f This is for form feed
`n This denotes new line
`r This is for carriage return
`t This is for horizontal tab
`u{x} This is for escape sequence of Unicode
`v This denotes vertical tab space

1. `0

This is used to insert an empty space in the PowerShell output. This is not the same as $null, which stores a null value. This is used for processing text files which has termination characters.

Code:

Write-Host "Example of null character escape sequene"
Write-Host "null" `0 "character"

Output:

`0

2. `a

This is used to produce a beep sound. This is used for warning purpose or to send a reminder to the user. The following script is used to produce three beep sounds.

Code:

Write-host “Alert sound example”
for ($i = 0; $i -le 2; $i++)
{
"`a"
}

Output:

`a

3. Backspace (`b)

This escape sequence is used to move the position of cursor one position back but characters are not deleted.

Code:

Write-Host "Example of back space escape sequene"
$input= "Vignesh Krishnakumar"
Write-Host "Actual word is "$input
Write-Host "after using back space"
Write-Host "vignesh`b`b`bkrishnakumar"

Output:

Backspace (`b)

4. Escape (`e)

This is used for formatting the text purpose like modifying the color or underlining the text. These can also be used for positioning the cursor.

5. Form Feed (`f)

This is used during printing of documents. This ejects the current page and enables continuous printing.

6. New line(`n)

This is used for inserting a new line immediately after the preceding character.

Code:

Write-Host "new line escape sequence example"
Write-Host "first line `nsecond line `nthird line"

Output:

New line(`n)

7. Carriage return(`r)

This is used to erase the line before the insertion point.

Code:

Write-Host "carriage return escape sequence example"
Write-Host "first line second line third line "
write-host "deleting everything in above example"
Write-Host "`rprevious characters are deleted"

Output:

Carriage return(`r)

8. Horizontal tab (`t)

This is used for inserting a tab space in the output. By default, PowerShell introduces a tab every eight space.

Code:

Write-Host "horizontal escape sequence example"
Write-Host "first word `t second word `t vignesh `t krishnakumar"

Output:

Horizontal tab (`t)

9. Stop-Parsing token (- -%)

This sequence is used when there is a need to pass input to other cmdlets. This prevents the PowerShell from interpreting strings as cmdlets or expressions.

Code:

Write-Host "PowerShell escape sequence example"
Write-Host "escaping # character"
echo one `# two
Write-Host "new line escape sequence"
Write-Host "one `ntwo `nthree"
Write-Host "horizontal tab escape sequence"
write-Host "vignesh`tkrishnakumar`tis working`tfrom chennai `the is `t a freelancer"
Write-Host "demo of semicolon to split strings"
Write-Host "first word ; second word"
Write-Host "escaping single and double quotes"
$msg1 = 'Every "man" should pay $5000 to get "free" food'
write-Host "messages is" $msg1
$msg2="Every ""man"" should bring `$5"
write-Host "message is" $msg2

Output:

PowerShell Escape Character - 8

Code:

Write-Host "PowerShell escape sequence example"
Write-Host "escaping # character"
echo 33#44#55
Write-Host "new line escape sequence"
Write-Host "first word `second word `third word"
Write-Host "horizontal tab escape sequence"
write-Host "this`is`tis working`as dfsd `sdfdsf is `t dsa sdf"
Write-Host "demo of semicolon to split strings"
Write-Host "dan brown ; j k rolwing"
Write-Host "escaping single and double quotes"
$msg1 = 'Every "sdfdsf" sfdsf pay $33434 to sdfdsf "sdfdf" sfdsdf'
write-Host "messages is" $msg1
$msg2="sdfdsf ""sfdsf"" sfdsf sfdf `$5234324"
write-Host "message is" $msg2

Output:

PowerShell Escape Character - 9

How to Handle Common Error?

Code:

write-host "Demo of single and double quotes"
write-output -inputobject 'My favorite `tteacher is 'raji''
Write-Host "The above will throw an error"
Write-Host "Correct usage is as below"
write-output -inputobject "My favorite `tteacher is 'raji'"

Output:

PowerShell Escape Character - 10

The error is thrown because a single quote is enclosed within a single quote. That can be avoided by using single quotes within double quotes.

Code:

Write-Host "Error with space example"
write-output -inputobject vignesh krishnakumar
Write-Host "Error is thrown"
Write-Host "Correct usage is as follows"
Write-Output -inputobject vignesh` Krishnakumar

Output:

PowerShell Escape Character - 11

The error has occurred the space has not been instructed properly to the PowerShell.

Escaping Special Characters using Regex

Code:

Write-Host "Escaping special characters demo"
Write-Host "Escaping backslash in path"
[regex]::Escape('C:\Vignesh\New folder')
Write-Host "Escape a string"
[regex]::Escape('Vignesh!!!Krishnakumar!!!')
[regex]::Escape('What is your age?')

Output:

PowerShell Escape Character - 12

Conclusion

Thus, the article covered in detail about the escape characters in PowerShell. It also covered in detail the various PowerShell escape sequences that are available and illustrated each with a detailed example. It also explained in detail the various errors that may occur during single quotes and double quotes usage and how to handle them. The article also covered in-depth about how special characters can be escaped and demonstrated it with proper examples. It showed an example of how regex can be used to avoid special characters. To learn in detail, it is advisable to write sample scripts and have a workaround with them.

Recommended Articles

This is a guide to PowerShell Escape Character. Here we discuss the escape sequence available in PowerShell with knowing how to handle it. You can also go through our other related articles to learn more –

  1. PowerShell String Replace
  2. PowerShell Get-Process
  3. PowerShell Format Table
  4. PowerShell Import Module
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