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

PowerShell – Multiline Command [With Examples]

Readable code is very easily communicates its purpose of functionality to the users. For code readability, variable names, method name should have proper naming convention. Other attributes which contributes to code readability is consistent indentation, formatting style for code. PowerShell multiline command helps to split commands into multiple statement for readability.

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

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

In this article, I will explain you how to use PowerShell multiline command to split long command over multiple lines.

PowerShell Multiline Command

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

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

We can easily split long command into multiple line using ` (PowerShell backtick character) for line break in given 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 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 long command into multiple line using space followed by ` (backtick character) at the end where we want to split it.

Using multiline command, its easily readable and easy to maintain in PowerShell.

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

PowerShell multiline command with comments

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

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

Lets understand it with long command to split into multiple lines and add comments in 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, its a long command which need to be split into multiple line using ` (backtick character). In the example, it gets free disk space for C drive.

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

Let’s consider, 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 ` backtick character splits long command over multiline command and using <# comment #> multiline comment out code.

Execute Multiline Command in One line

To execute multiline command in one line in PowerShell, use 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 seperated by semicolon (;)

These commands 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 pipe operator (|) to chain commands and execute it 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, first command uses Get-AdGroupMember cmdlet to get ad groups members for group Administrator and pass output to second command using pipe operator.

Second command uses Export-Csv cmdlet to export adgroup members to csv file.

Cool Tip: How to add newline to string or variable in PowerShell!

PowerShell Multi line Command in Dockerfile

To run multi line docker run command in PowerShell, use 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 above article on Powershell multiline command with comments useful and educational.

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

Read here about how to create multiline string in PowerShell!

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

Leave a Comment