PowerShell


Windows PowerShell Backtick `

Introduction to Windows PowerShell Backtick `

It has to be said that if you blink, then you will miss the ` backtick.  However, it would be a pity if did, because backtick a

llows you to word-wrap PowerShell commands.

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.

Example 1: PowerShell's Backtick

# cls
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: 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 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

Summary of PowerShell's Backtick

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 backtick family, for example `t to align an output column with a tab.

See Also

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

WebComputerperformance.co.uk

 

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

Please report a broken link, or an error.