Windows PowerShell


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, it would be a pity if did, because backtick allows you to word-wrap PowerShell commands.  Incidentally, some people call this the grave key.

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 the end of 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 wraps to the next line - enter the tiny backtick `.

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 1: The PowerShell Backtick in Action

# PowerShell backtick ` 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 2: The PowerShell Backtick as an escape character

It is important to distinguish the primary use of the backtick, on its own `, and its secondary use as an escape character, for example, backtick with `n or `t .  Let me explain with an example.

2a) Write-host "Heading Sub Heading" 

Problem: undesired result

Heading Sub Heading

Solution: Add the crucial `n

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

Result: Desired result achieved

Heading
Sub Heading

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

  ˚

Meet the rest of the PowerShell Backtick family

`   Plain backtick, classic word-wrap symbol (See example 1)

`t  'T' for tab

`n  'N' for new line

`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

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Summary of PowerShell's Backtick Operator

Take the time to seek out the `backtick key.  One day it will help you 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.

See more Microsoft PowerShell tutorials

Windows PowerShell Home  • Introduction  • Cmdlets  • Exchange 2007  • Profile.ps1  • $_.Pipeline

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.