Windows PowerShell

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



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

# 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?

Problem with PowerShell's Backtick and Whitespace

There is a really nasty problem with the PowerShell backtick, and that is if 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

Clarification of the problem.  Get PowerShell word-wrap working properly with the backtick.  Then to see the problem press the spacebar after the backtick and try again.  You should get an error similar to the message above.

# Correct no whitespace
Get-Service * |Sort-Object ServiceType `
# Problem whitespace (x6 to exaggerate)
Get-Service * |Sort-Object ServiceType `     
# Representation of the problem whitespace with a dot
Get-Service * |Sort-Object ServiceType `........

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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

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" 

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

Heading
Sub Heading

Note 1: 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 backtick instead.

# PowerShell Backtick for Space
Clear-Host
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 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

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 teach me more about how the system itself 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

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.

See more Windows PowerShell tutorials

PShell Home  • Introduction  • Dreams  • 3 Key Commands  • Cmdlet scripts  • PowerShell examples

Process example  • Backtick  • Get-Command  • PowerShell ISE  • Get-Member  • Set-ExecutionPolicy

Please write in 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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

WebThis Site

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

 Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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

 

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

Please report a broken link, or an error.