Skip navigation

Breaking Lines in PowerShell: Lose the Backtick

Sometimes, an entire PowerShell command can't fit comfortably on a line. That's especially true when you're blogging, or writing books. Take this lengthy command as an example:

Get-WmiObject -class Win32_OperatingSystem -computername (Get-Content names.txt) | Where-Object { $_.BuildNumber -eq 7600 -and $_.ServicePackMajorVersion -lt 1 } | Select-Object -Property @{name='ComputerName';expression={$_.__SERVER}},Caption,ServicePackMajorVersion | Export-CSV needs_updated.csv

Sometimes, you just need to break that across multiple lines. I used to be guilty of this kind of thing:

Get-WmiObject -class Win32_OperatingSystem -computername (Get-Content names.txt) | `
Where-Object { $_.BuildNumber -eq 7600 -and $_.ServicePackMajorVersion -lt 1 } | `
Select-Object -Property @{name='ComputerName';expression={$_.__SERVER}}, `
 Caption,ServicePackMajorVersion | `
Export-CSV needs_updated.csv

Those backticks are PowerShell's escape character. In this instance, they're escaping the carriage return at the end of the line, turning it from a functional end-of-command indicator into a literal carriage return. In other words, it's serving as a "line continuation" character of sorts. There are two major problems with this approach, I've realized:
  • That backtick character is really hard to read. In a book, it's easy to mistake it for a stray piece of ink or toner. On-screen, it's just easy to miss. It's, like, 3 pixels. 
  • If there's any white space after the backtick - a space or tab, for example - then THAT will be escaped, not the carriage return. That fact makes it very easy to mess up when you're typing a command in, with the result being improper execution and unexpected error messages.
That's when I realize how smart PowerShell is. Anytime it sees an opening item such as { [ ( ' or " it will let you hit Enter all you want, without actually ending the line, since it knows that you've got more to type. It'll also allow you to hit enter, creating a new physical line but not executing the command, when a line ends in a pipe | or comma character - even if those characters are followed by other whitespace. In other words, I could neatly format my command by doing this:

Get-WmiObject -class Win32_OperatingSystem -computername (Get-Content names.txt) | 
Where-Object { 
   $_.BuildNumber -eq 7600 -and $_.ServicePackMajorVersion -lt 1 
} | 
Select-Object -Property @{name='ComputerName';expression={$_.__SERVER}},
 Caption,ServicePackMajorVersion | 
Export-CSV needs_updated.csv

That might be overkill, but you can see how the shell permits it, either in a script or even interactively at the command-line. Try it - at the command-line you'll get a >> prompt after typing { and hitting Enter. Continue typing exactly as shown here, and when you're done hit Enter on a blank >> prompt to execute the command.

So you CAN neatly-format your commands and code without having to drag that difficult-to-see backtick ` character into the picture.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish