8

I can run the following command from PowerShell to start another PowerShell instance, change the window name, then run ls command and open Chrome to a specific web site from the new PowerShell instance.

start powershell `
"-noexit", `
"`$host.ui.RawUI.WindowTitle` = 'list files and goto SO'; `
ls ;`
Start-Process chrome.exe https://stackoverflow.com/"

How do I do the same thing in the new Windows Terminal (wt)?

Running the following from PowerShell opens wt to the "Windows Powershell" profile and changes the title of the "Windows Powershell" tab.

start wt '-p "Windows PowerShell" --title  "list files and goto SO" '

However, I cannot pass in any additional commands to get executed in the Windows Terminal "Windows Powershell" profile shell.

Is this currently possible?

I am aware of the following: https://github.com/microsoft/terminal/pull/3495 https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows

1
  • 1
    Simply put, as you have discovered, you can't, because it's simply not an option.
    – postanote
    Commented Jun 27, 2020 at 2:32

1 Answer 1

6

The Using command-line arguments for Windows Terminal article appears to me a bit unclear (or even confusing). However, the (default) wt command new-tab offers the commandline parameter along with (or instead of?) the -p profile-name one. So use a command line as defined by powershell.exe -Help. Something like

  • wt PowerShell.exe -NoExit -Command "& {$Host}" from Windows cmd command prompt, or
  • wt.exe PowerShell.exe -NoExit -Command "& {`$Host}" from an open PowerShell session (note escaped dollar sign and explicit use of the .exe file extension in wt.exe).

BTW, I don't see any difference between wt PowerShell.exe -NoExit -Command "& {$Host}" and wt -p "Windows PowerShell" PowerShell.exe -NoExit -Command "& {$Host}". In both cases, the PowerShell starts in a wt tab… It's because I have default profile set to Windows PowerShell in settings.json under %LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\.

Unfortunately, a semicolon (;) is used in a wt instance to create a new tab; hence, it's not applicable as a PowerShell command separator. Therefore,

  • wt PowerShell.exe -NoExit -Command "& {$Host; $PWD}" will fail. I don't know how-to escape that however I know a workaround:
  • wt PowerShell.exe -NoExit -Command "& {$Host, $PWD}"; although doing that is still possible, I found a regular solution recently: use the \ (backslash) as an escape character for the ; (semicolon) it wt parameters:
  • wt PowerShell.exe -NoExit -Command "& {$Host\; $PWD}".

For more complex/advanced commands, apply the Grouping operator ( ) as follows in a use case similar to yours (run from from an open PowerShell session):

wt.exe PowerShell.exe -NoExit -Command "& {(`$Host.UI.RawUI.WindowTitle='list files and goto SO'),`$PWD.Path,(Push-Location D:\Downloads),(ls e*.ps1),(Start-Process -PassThru chrome.exe https://stackoverflow.com/)}"

with the following result in Windows Terminal:

Windows Terminal

Above code will suspend parent Powershell until the terminal is closed; if you want to continue in parent Powershell then use

Start-Process wt.exe -ArgumentList "PowerShell.exe", "-NoExit", "-Command", "& {(`$Host.UI.RawUI.WindowTitle='list files and goto SO'),`$PWD.Path,(Push-Location D:\Downloads),(ls e*.ps1),(Start-Process -PassThru chrome.exe https://stackoverflow.com/)}"

Edit:

Windows Terminal uses the \ (backslash) as an escape character for the ; (semicolon). Hence, the latter workaround I replace with equivalent regular solving:

Start-Process wt.exe -ArgumentList "PowerShell.exe", "-NoExit", "-Command", "& {`$Host.UI.RawUI.WindowTitle='list files and goto SO'\;`$PWD.Path\;Push-Location D:\Downloads\;ls e*.ps1\;Start-Process -PassThru chrome.exe https://stackoverflow.com/}"

or, with -p flag:

Start-Process wt.exe -ArgumentList '-p "Windows PowerShell"', "PowerShell.exe", "-NoExit", "-Command", "& {`$Host.UI.RawUI.WindowTitle='list files and goto SO'\;`$PWD.Path\;Push-Location D:\Downloads\;ls e*.ps1\;Start-Process -PassThru chrome.exe https://stackoverflow.com/}"
6
  • Excellent answer. Now if only there was a way to get the command to open in a new tab in the same wt instance.
    – Clay
    Commented Jun 27, 2020 at 18:03
  • wt PowerShell.exe -NoExit -Command "& {$Host.UI\; $PWD.Path}"; PowerShell.exe -NoExit -Command "& {$PWD.Path\;$Host.UI}" or something similar?
    – JosefZ
    Commented Jun 28, 2020 at 14:31
  • 1
    This creates a new Windows Terminal with two tabs that run the respective commands, but it does not launch a new tab within the existing Windows Terminal host. start wt "PowerShell.exe", "-NoExit", "& {`$Host.UI\; `$PWD.Path}", ';', "PowerShell.exe", "-NoExit", "& {`$PWD.Path\;`$Host.UI}"
    – Clay
    Commented Jun 28, 2020 at 18:04
  • @Clay please ask another question about this request (which extends topic of the current one)…
    – JosefZ
    Commented Jun 28, 2020 at 19:31
  • backslash worked for me.
    – Tiju John
    Commented Jan 18 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .