3 Approaches to Powershell Multiliners — Backticks, Splatting, and Natural Line Continuators

usecase

  • the question is that of an elegant interaction, i.e. not having to write long commands in a single line
  • the answer is don’t use backticks unless splitting arguments, try splatting and rely on natural line continuators like a pipeline operator or scriptblock operator (and many others)

1. backtick

  • a cmdlet ` Get-ChildItem -Path $env:windir -Filter *.dll -Recurse` can be made a little more readable by breaking up long lines with PowerShell’s escape character:
  • backtick is the escape character in powershell

In general, the community feels you should avoid using those backticks as “line continuation characters” when possible.

Readability · PowerShell Practice and Style

  • hard to read
  • easy to miss
  • easy to mistype
  • when needing to split line into several cmdlet arguments
  • not piping
  • not using script blocks
  • …but say copying the content of an entire folder (files and subfolders)
  • this is just for the “personal hygiene”, not communicating with another person (this is not code, this is an interactive shell where I just communicate with myself)

2. splatting

  • splatting means passing parameters bound via a hash table to command with @ operator

3. natural line continuation

These are parts of the PowerShell language that naturally allow you to continue on to the next line without any special character or consideration.

Bye Bye Backtick: Natural Line Continuations in PowerShell

  • highly compatible with K&R indentation / 1TBS
  • in the following, I am utilizing SCRIPTBLOCK OPERATOR {} and PIPELINE OPERATOR |
  • I am just copying the last 3 downloaded files into its ticket-folder

4. sources

today I learnt… | as a support eng of the wonderful Snowplow Analytics, expect everything around modern (postmodern?) business intelligence

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store