How to pass commands into the shell opened in new Windows Terminal
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 Windowscmd
command prompt, or -
wt.exe PowerShell.exe -NoExit -Command "& {`$Host}"
from an openPowerShell
session (note escaped dollar sign and explicit use of the.exe
file extension inwt.exe
).
BTW, I don't see any difference between It's because I have default profile set to Windows PowerShell in 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…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 thathowever 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) itwt
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:
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/}"