Split Commands to Multiple Lines in PowerShell

Marion Paul Kenneth Mendoza Jan 22, 2022 Jan 13, 2022
  1. Windows PowerShell Multiline Command
  2. Breaking Long Lines of Code Using Specific Operators
Split Commands to Multiple Lines in PowerShell

Readable code is very easily communicated its purpose of functionality to the users. Variable names and method names should have proper naming conventions for code readability. Other attributes that contribute to code readability are consistent indentation and formatting style. Windows PowerShell multiline command helps split long command lines into multiple statements for readability.

In Windows PowerShell, multiline commands can be easily created using the backtick character to split long or single-line commands multiline statements.

The backtick character is used as a kind of escape character. It escapes the newline character and results in line continuation.

This article will explain the importance of Windows PowerShell multiline commands to split long commands over multiple lines.

Windows PowerShell Multiline Command

To split a long command into multiple lines, use the backtick character to break it into multiple lines.

For example, we want to get the free disk space information on the local computer. Unfortunately, the script to get this specific information is an extended command, making it difficult to read and manage.

Example Code:

Get-WmiObject -Class win32_logicaldisk | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

It may look like the syntax is split in this article, but when copied to the command-line interface, the example above is a very long one-liner script.

We can easily split long command into multiple lines using Windows PowerShell backtick character for a line break in a given command.

Example Code:

Get-WmiObject -Class win32_logicaldisk `
| Format-Table DeviceId, `MediaType, @{n="Size";e={[Math]::Round($_.Size/1GB,2)}}, `
@{n="FreeSpace";e={[Math]::Round($_.FreeSpace/1GB,2)}}

In the above example, we split the long command into multiple lines using a white space followed by the backtick character at the end where we want to split it. Remember that there must not be any character between the backtick and the line break, including white spaces.

The code structure is easily readable and easy to maintain in Windows PowerShell using the multiline command. However, the backtick character is not usually recommended because the character is hard-to-read and invites bugs. So, we have an alternate method of breaking long lines of code.

Breaking Long Lines of Code Using Specific Operators

Usually, you get automatic line continuation when a command cannot syntactically be complete at that point.

One example would be starting a new pipeline element (|). The pipeline will work without problems since, after the pipeline operator, the command cannot be complete since it’s missing another pipeline element. So what our interpreter does is look for the next pipeline element in the following command line.

Example Code:

Get-WmiObject -Class win32_logicaldisk | 
Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

A comma (,) will also work in some context like the pipeline operator.

Example Code:

Get-WmiObject -Class win32_logicaldisk | 
Format-Table DeviceId, MediaType, 
@{n="Size"; e={[Math]::Round($_.Size/1GB,2)}},
@{n="FreeSpace"; e={[Math]::Round($_.FreeSpace/1GB,2)}}

Also, curly braces ({}) when defining script blocks will allow line continuation directly.

Example Code:

Get-WmiObject -Class win32_logicaldisk | 
Format-Table DeviceId, MediaType, 
@{
    n="Size"; e={
        [Math]::Round($_.Size/1GB,2)
    }
},
@{
    n="FreeSpace";e={
        [Math]::Round($_.FreeSpace/1GB,2)
    }
}

Using the techniques above, we can now observe that our example code is easier to read, and we managed this without using the backtick character.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn