1

I want to run the following command from the powershell window.

My-Command1 -f “firstline
secondline
thirdline”

I want to be able to copy and paste in a bunch of lines for the -f parameter - can this be done?

I tried copy and pasting but it didn’t work.

1 Answers1

1

Yes, you can use and paste multi-line arguments, but only if provided as string literals, i.e with delimiters (quotes) - see the bottom section re pasting multi-line commands.

Important:

  • On Windows, if you use Ctrl-V to paste - rather than right-clicking - you can simplify the solutions below by typing both the opening and closing delimiter up front and then placing the cursor between them before pasting.

  • For the reasons discussed in GitHub issue #579 this does not work in the following cases, because these pasting methods use simulated typing:

    • on Windows if right-clicking is used.
    • on Unix-like platforms, invariably, as of this writing (however, bracketed pasting may offer a solution in the future).

In the simplest case, type the opening delimiter (" or, preferably, for literal content, '), then paste your multi-line value, then type the closing delimiter; e.g.:

Write-Output '  # <- paste here, then type ' to close the string

If there's a chance that the value you're pasting contains the same quote characters that you're using to delimit the string, use a here-string instead; e.g.:

Write-Output @' # <- Press ENTER, then paste, 
                # then press ENTER again, then type '@ to close the here-string

If you find yourself needing to paste multi-line values frequently, you can set up a custom PSReadLine key handler as follows:

# Define keyboard shortcut Alt-V to paste the current clipboard
# content as a verbatim here-string.
Set-PSReadLineKeyHandler 'alt+v' -ScriptBlock {
  [Microsoft.PowerShell.PSConsoleReadLine]::Insert("@'`n`n'@")
  foreach ($i in 1..3) { [Microsoft.PowerShell.PSConsoleReadLine]::BackwardChar() }
  [Microsoft.PowerShell.PSConsoleReadLine]::Insert((Get-Clipboard -Raw))
  foreach ($i in 1..3) { [Microsoft.PowerShell.PSConsoleReadLine]::ForwardChar() }
}

You can then use Alt-V to paste (multi-line) text currently on the clipboard as a verbatim here-string (customize the keyboard shortcut as needed).


Pasting multi-line PowerShell commands / code snippets:

It follows from the above:

  • On Windows, with Ctrl-V, you're free to paste arbitrary (but complete) code snippets and execute them.

  • On Unix-like platforms (and when you use right-clicking on Windows), in the absence of bracketed pasting, the snippet may be pasted as multiple commands, which can break:

    • As soon as a statement contained in the pasted text is a complete statement by itself, it is submitted.

    • A notable pitfall is that if an if statement's else branch is placed on its own line, the if branch is considered a complete statement and submitted first, causing a syntax error when the else branch is subsequently submitted; e.g.:

      # Pasting this currently malfunctions, except on 
      # Windows with Ctrl-V
      if ($true) {
        'yup'
      }
      else {
        'nah'
      }
      
mklement0
  • 382,024
  • 64
  • 607
  • 775