Home » PowerShell » PowerShell – Multiline Command [With Examples]

PowerShell – Multiline Command [With Examples]

PowerShell multiline command helps to split commands into multiple statements for readability.

Readable code very easily communicates its purpose of functionality to the users. For code readability, variable names, and method names should have proper naming conventions.

Other attributes which contribute to code readability are consistent indentation and formatting style for code.

In PowerShell multiline command can be easily created using ` (backtick character) to split long or a single line command into multiline statements.

Backtick (`) character is used as an escape character, it basically escapes the newline character and results in line continuation.

In this article, we will discuss how to use PowerShell multiline command to split long commands over multiple lines.

PowerShell Multiline Command

To split long command into multiple lines, use the backtick (`) character in the command where you want to split it into multiple lines.

Let’s consider an example, we want to get free disk space on a local computer name is a long command which makes it very difficult to read and manage.

We can easily split long commands into multiple lines using ` (PowerShell backtick character) for a line break in the following command

Get-CimInstance -ComputerName localhost win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}

After using ` (PowerShell backtick character) for a line break in the command, it can be easily read

Get-CimInstance -ComputerName localhost win32_logicaldisk `
| where caption -eq "C:" `
| foreach-object {write " $($_.caption) $('{0:N2}' `
-f ($_.Size/1gb)) GB total, $('{0:N2}' `
-f ($_.FreeSpace/1gb)) GB free "}

In the above example, we split the long command into multiple lines using space followed by ` (backtick character) at the end where we want to split it.

Using the multiline command, it’s easily readable and easy to maintain in PowerShell.

Cool Tip: Use Test Connection to ping a list of computers in PowerShell!

PowerShell multiline command with comments

In PowerShell scripting, we add comments in code to provide details about the command or function or comment out original code for testing purposes and use test code.

In PowerShell multiline commands with comments can be easily added using <# comment #> in code.

Let’s understand it with the long command to split into multiple lines and add comments in the script as below

Get-CimInstance -ComputerName localhost win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}

In the above PowerShell script, it’s a long command which needs to be split into multiple lines using ` (backtick character). In the example, it gets free disk space for the C drive.

Cool Tip: Fix running script is disabled on this system in PowerShell!

Let’s consider, that we need to comment our where caption -eq "C:" code for multiline command and use where caption -eq “D:”

Get-CimInstance -ComputerName localhost win32_logicaldisk `
<# | where caption -eq "C:" ` #> `
| where caption -eq "D:" `
| foreach-object {write " $($_.caption) $('{0:N2}' `
 -f ($_.Size/1gb)) GB total, $('{0:N2}' `
  -f ($_.FreeSpace/1gb)) GB free "}

In the above PowerShell multiline command example, PowerShell ‘s ` backtick character splits long command over multiline command and uses <# comment #> multiline comment out code.

Execute Multiline Command in One line

To execute multiline command in one line in PowerShell, use a semicolon (;) after each command.

Let’s consider an example below to run multiple commands in one line

Write-Host "Get File version";$fileversion =(Get-Command D:\PowerShell\ShellWatcher.dll).FileVersionInfo.FileVersion;Write-Host "File version -" $fileversion  

In the above PowerShell multiple commands example, it has 3 commands separated by semicolon (;)

These commands are executed in sequence, print message and get file version and print output as below

PowerShell multiline command in one line
PowerShell multiline command in one line

You can also use the pipe operator (|) to chain commands and execute them based on conditions.

For example, get ad group members from active directory and export group members to CSV file, use below multiple commands in one line separated by pipe operator

Get-AdGroupMember -Identity 'Administrators' | Export-csv -Path D:\Powershell\adgroupmemers.csv  -NoTypeInformation

In the above PowerShell script, the first command uses the Get-AdGroupMember cmdlet to get ad groups members for the group Administrator and pass output to the second command using the pipe operator.

The second command uses the Export-Csv cmdlet to export adgroup members to a CSV file.

Cool Tip: How to add a new line to a string or variable in PowerShell!

PowerShell Multi line Command in Dockerfile

To run multi-line docker run command in PowerShell, use the backtick (`) character at the end of each line in Dockerfile.

for example, below Dockerfile illustrates multi line docker run command

docker run -p 80:8080 -p 443:443 `
           -h hostname.domain `
           -e "MYSQL_ROOT_PASSWORD=password" `

Conclusion

I hope you found the above article on Powershell multiline command with comments useful and educational.

Using ` (backtick character), the long command breaks into multiple lines, and using <# comment #> we can add comments in the multiline command.

Read here about how to create a multiline string in PowerShell!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.