Search code examples
powershellwindows-terminal

trouble with calling pwsh through windows terminal command line


This is something I have been struggling with for a few days and each time I think I have adjourned it, it falls apart.

I want to launch a new Windows Terminal window but with two caveats:

  1. The new terminal must be a windows terminal profile
  2. At the same time, pass a command line argument to the WT profile's shell, which is pwsh, so pwsh's -command parameter

A few days ago, I figured out how to send command line arguments to the profiles shell, Pass a command line argument to the new terminal window that is about to be launched? #17521.

This successfully opens a new terminal window with my_profile profile. The host prints my message:

wt -p my_profile pwsh -noExit -command "'my message'"

All was good and well until it broke on me today. The following does open a new terminal window but the window prints an error:

wt -p my_profile pwsh -noExit -command "'my message' ; 'my second message'"

The error is printed by windows terminal (not pwsh):

[error 2147942402 (0x80070002) when launching `" 'my second message'"']

If I eliminate WT from the equation, pwsh correctly receives input to its -command parameter:

pwsh -noExit -command "'my message' ; 'my second message'"


#my message
#my second message

Is pwsh really not at fault here?

Is there some sort of pwsh mechanism that can help me to navigate around this issue?

I would appreciate any solutions in either pwsh or wt

Windows Terminal 1.19 preview

pwsh 7.4

win 11


Solution

  • Windows Terminal uses ; to separate "actions" in the wt command line:

    Command line syntax

    The wt command line accepts two types of values: options and commands.

    • Options are a list of flags and other parameters that can control the behavior of the wt command line as a whole.

    • Commands provide the action, or list of actions separated by semicolons, that should be implemented. If no command is specified, then the command is assumed to be new-tab by default.

    So, if you look back at your wt command line:

    wt -p my_profile pwsh -noExit -command "'my message' ; 'my second message'"
    

    Windows Terminal is trying to run the equivalent of:

    wt -p my_profile ; new-tab pwsh -noExit -command "'my message' ; new-tab 'my second message'"
    
    first  "action":           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    second "action":                                                         ^^^^^^^^^^^^^^^^^^^^
    

    and you end up with a new Windows Terminal with two tabs - one for each new-tab "action":

    • pwsh -noExit -command "'my message' - the first tab has the output my message - note that even though the " quotes in the -command argument aren't closed properly, powershell still executes it fine (you can try it from a command prompt, but not an interactive powershell session)

    • 'my second message'" - the second tab has the error message [error 2147942402 (0x80070002) when launching `" 'my second message'"'] because 'my second message'" isn't a valid command. (Note the extra leading " in the error - I'm presuming Windows Terminal has added this because the second command starts with a space character, but I'm not sure tbh).

    Workaround

    I couldn't find it documented, but I tried escaping ; as \; which seems to work - wt will then treat the \; as a literal ; in the "action" command:

    wt -p my_profile pwsh -noExit -command "'my message' \; 'my second message'"
    
    single action:   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    and you end up with a single tab with the output:

    my message
    my second message
    

    Note - all of the above was tested in a Windows Command Prompt - it's possible different escaping rules will apply depending on where you're calling wt from...