[powershell] How to enter a multi-line command

Is it possible to split a PowerShell command line over multiple lines?

In Visual Basic I can use the underscore (_) to continue the command in the next line.

This question is related to powershell multiline

The answer is


Just use ` character to separate command on multiline


Just add a corner case here. It might save you 5 minutes. If you use a chain of actions, you need to put "." at the end of line, leave a space followed by the "`" (backtick). I found this out the hard way.

$yourString = "HELLO world! POWERSHELL!". `
                  Replace("HELLO", "Hello"). `
                  Replace("POWERSHELL", "Powershell")

To expand on cristobalito's answer:

I assume you're talking about on the command-line - if it's in a script, then a new-line >acts as a command delimiter.

On the command line, use a semi-colon ';'

For example:

Sign a PowerShell script on the command-line. No line breaks.

powershell -Command "&{$cert=Get-ChildItem –Path cert:\CurrentUser\my -codeSigningCert ; Set-AuthenticodeSignature -filepath Z:\test.ps1 -Cert $cert}

In PowerShell and PowerShell ISE, it is also possible to use Shift + Enter at the end of each line for multiline editing (instead of standard backtick `).


If you are trying to separate strings into multiple lines, you can use the "+". For example:

$header =    "Make," +

             "ComputerName," +

             "Model," +

             "Windows Version"

Will look just like:

$header = "Make,ComputerName,Model,Windows Version"

There's sooo many ways to continue a line in powershell, with pipes, brackets, parentheses, operators, dots, even with a comma. Here's a blog about it: https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html

You can continue right after statements like foreach and if as well.


I just found out that there must not be any character between the back tick and the line break. Even whitespace will cause the command to not work.


In most C-like languages I am deliberate about placing my braces where I think they make the code easiest to read.

PowerShell's parser recognizes when a statement clearly isn't complete, and looks to the next line. For example, imagine a cmdlet that takes an optional script block parameter:

    Get-Foo { ............ }

if the script block is very long, you might want to write:

    Get-Foo
    {
        ...............
        ...............
        ...............
    }

But this won't work: the parser will see two statements. The first is Get-Foo and the second is a script block. Instead, I write:

    Get-Foo {
        ...............
        ...............
        ...............
    }

I could use the line-continuation character (`) but that makes for hard-to-read code, and invites bugs.

Because this case requires the open brace to be on the previous line, I follow that pattern everywhere:

    if (condition) {
        .....
    }

Note that if statements require a script block in the language grammar, so the parser will look on the next line for the script block, but for consistency, I keep the open brace on the same line.

Simlarly, in the case of long pipelines, I break after the pipe character (|):

    $project.Items | 
        ? { $_.Key -eq "ProjectFile" } | 
        % { $_.Value } | 
        % { $_.EvaluatedInclude } |
        % {
            .........
        }

$scriptBlock = [Scriptblock]::Create(@'
  echo 'before'
  ipconfig /all
  echo 'after'
'@)

Invoke-Command -ComputerName AD01 -ScriptBlock $scriptBlock

source
don't use backquote


I assume you're talking about on the command-line - if it's in a script, then a new-line acts as a command delimiter.

On the command line, use a semi-colon ';'


  1. Use a semi-colon ; to separate command
  2. Replace double backslash \\ on any backslashes \.
  3. Use "' for passing safe address to switch command like "'PATH'".

This ps1 command install locale pfx certificate.

powershell -Command "$pfxPassword = ConvertTo-SecureString -String "12345678" -Force -AsPlainText ; Import-PfxCertificate -FilePath "'C:\\Program Files\\VpnManagement\\resources\\assets\\cert\\localhost.pfx'" Cert:\\LocalMachine\\My -Password $pfxPassword ; Import-PfxCertificate -FilePath "'C:\\Program Files\\VpnManagement\\resources\\assets\\cert\\localhost.pfx'" Cert:\\LocalMachine\\Root -Password $pfxPassword"

I started by doing

if ($true) {
"you can write multiple lines here, and the command doesn't run untill you close the bracket"

"like this"
}

Recently found out I could just

&{
get-date
"more stuff"
}