Windows PowerShell Backtick `

Introduction To the Windows PowerShell Backtick Operator `

It has to be said that if you blink, then you will miss the ` backtick operator.  However, that would be a pity, because backtick enables you to escape characters, for example the carriage return, and thus achieve word-wrap in your PowerShell commands.  Incidentally, some people call this the grave key.

Topics for PowerShell's Backtick `


Where is the Backtick Key?Backtick key in PowerShell

On my UK keyboard the backtick is on the top row next to '1' key.  It's the key above the Tab and below the Esc key.  My key has three symbols, ` ¬  ¦ All you need to get the backtick is press the key on its own without holding down the shift or the Alt key.

Any doubt about which is the backtick key, hold down the Alt and press these four numbers on the number pad: 0096, only now let go of the Alt key.

Example 1a: The PowerShell Backtick in Action

When you write code for any scripting language there comes a time when you want the command to wrap onto the next line.  PowerShell, along with most languages, regards the end-of-line character as terminating that command.  Consequently, a new line means the start of a new command.  Thus we have established the need for a command to tell PowerShell that this command continues onto the next line - enter the tiny backtick `.

# Backtick ` PowerShell word-wrap
Get-Service * |Sort-Object ServiceType `
| Format-Table name, status, `
 ServiceType, CanStop -auto

Note 1: The full command if written on one line would be:

Get-Service * |Sort-Object ServiceType | Format-Table name, ServiceType, status, CanStop, -auto

As you can see, unless I use the smallest font, it won't all fit on one line, consequently we need to tell PowerShell to word-wrap.  More importantly, most script editors issue an end-of-line marker before they reach 86 characters.

Challenge: There is a ` backtick after ServiceType, but can you spot a second backtick?

Example 1b: No Backtick, Place the | Pipe Carefully

# No backtick needed, instead position the (|) at end of the line.
Get-Service * |
Sort-Object ServiceType |
Format-Table name, status,  ServiceType, CanStop -auto

Note 2: PowerShell's intelligence understands that there must more after the |, and carries on interpretting the rest of command on the next line.

 Compare the position of the (|) in Example 1b with 1a.  Incidentally, this explains why you don't need the backtick after an open bracket ({ or [.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds' Network Performance Monitor will help you discover what's happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds' Network Performance Monitor

Problem with PowerShell's Backtick and Whitespace

The error message: 'An empty pipe element is not allowed', is a really nasty problem with the PowerShell backtick.  It happens when you inadvertently add whitespace after the ` and before the actual end of the line. 

# Backtick ` Problem white space after the backtick.
Get-Service * | Sort-Object ServiceType ` 
| Format-Table name, status, `
 ServiceType, CanStop -auto

Example of the error message

An empty pipe element is not allowed.
At line:3 char:1

# The root of this problem is that the backtick is escaping whitespace, whereas we want it to escape the carriage return.

To create the problem, firstly, get PowerShell's word-wrap working properly with the backtick.  Then in order to see the error, press the spacebar after the backtick and try again.  You should get an 'empty pipe' error similar to the message above.

Here below is a representation of the above whitespace problem with dots.

Get-Service * | Sort-Object ServiceType `........
| Format-Table Name, ServiceType, Status -AutoSize

Good news: Later versions of PowerShell compensate for this space, and consequently, you see less of the 'empty pipe' error.

More good news: Judicious placement of the | pipe command means less need for the ` backtick to escape the carriage return.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Example 2: Other Uses of Backtick as an Escape Character

My first use of the ` backtick in PowerShell was to word-wrap.  Secondly, I discovered that the ` backtick can be used in conjunction with 'n' or 't' to format text with a new line (`n), or a tab (`t).

2a) Write-host "Heading Sub Heading" 

# PowerShell New Line Problem
Write-host "Heading Sub Heading"

Problem: An undesired result because the text is all on the same line.

Heading Sub Heading

Solution: Add the crucial `n

2b) Write-host "Heading `nSub Heading"

# PowerShell New Line solution
Write-host "Heading `nSub Heading"

Result: Desired result achieved:

Sub Heading

Note 3: Remember that the escape family `n, `r and `t work within "text" rather than within command statements.

2c) Backtick instead of "Quotes"

Take a directory such as "Program Files".  I would script the path with those speech marks; however, it's possible to use no quotation marks, and instead employ the backtick.

# PowerShell Backtick for Space
Get-ChildItem C:\Program` Files\

Guy would use Get-ChildItem "C:\Program Files\" instead of a backtick to escape the whitespace.

Meet the Rest of the PowerShell Backtick family

`  Plain backtick, classic PowerShell word-wrap symbol (See above Example 1)

`t  'T' for tab

`n  'N' for a new line in PowerShell

`r   'R' for return as in carriage return

Backtick also has distant cousins who I have heard about, but never met.

`a  Alert

`b  Backspace

`0  (Zero) Null

`'  Speech mark

`" Another speech mark

More Examples of Backtick in Action

Summary of PowerShell's Backtick Operator

Take the time to seek out the `backtick key.  One day this tiny symbol will help PowerShell to word-wrap your commands.  There may also be work for the rest of the PowerShell backtick family, for example `t to align an output column with a tab.

If you like this page then please share it with your friends


See more Windows PowerShell tutorials

PShell Home   • Introduction   • Dreams   • 3 Key Commands   • PowerShell Help About   • Get-Help

PowerShell v 3.0   • Set-ExecutionPolicy   • Get-Command   • Cmdlet scripts   • Import-Module

PowerShell Version Check   • Backtick   • PowerShell examples   • PowerShell ISE   • Get-Member

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.



Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2016 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: