如何将命令传递到在新 Windows 终端中打开的 shell

Cla*_*lay 5 powershell windows-10 windows-terminal

我可以从 PowerShell 运行以下命令来启动另一个 PowerShell 实例,更改窗口名称,然后运行ls命令并从新的 PowerShell 实例打开 Chrome 到特定网站。

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

我如何在新的 Windows 终端 (wt) 中做同样的事情?

从 PowerShell 运行以下命令会打开 wt 到“Windows Powershell”配置文件并更改“Windows Powershell”选项卡的标题。

start wt '-p "Windows PowerShell" --title  "list files and goto SO" '
Run Code Online (Sandbox Code Playgroud)

但是,我无法传入任何其他命令以在 Windows 终端“Windows Powershell”配置文件外壳中执行。

这是目前可能的吗?

我知道以下内容:https : //github.com/microsoft/terminal/pull/3495 https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows

Jos*_*efZ 5

在我看来,使用Windows 终端的命令行参数一文有点不清楚(甚至令人困惑)。但是,(默认)wt命令new-tab提供commandline参数以及(或代替?)参数-p profile-name。因此,请使用所定义的命令行powershell.exe -Help。就像是

\n
    \n
  • wt PowerShell.exe -NoExit -Command "& {$Host}"从 Windowscmd命令提示符,或
  • \n
  • wt.exe PowerShell.exe -NoExit -Command "& {`$Host}"来自打开的PowerShell会话(注意转义的美元符号显式使用.exe中的文件扩展名wt.exe)。
  • \n
\n

顺便说一句,我没有看到wt PowerShell.exe -NoExit -Command "& {$Host}"和之间有任何区别wt -p "Windows PowerShell" PowerShell.exe -NoExit -Command "& {$Host}"在这两种情况下,都会PowerShellwt选项卡\xe2\x80\xa6中启动,这是因为我在下将默认配置文件设置为Windows PowerShellsettings.json%LOCALAPPDATA%\\Packages\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\LocalState\\

\n

;不幸的是,在实例中使用分号( )wt来创建新选项卡;因此,它不适用于 PowerShell 命令分隔符。所以,

\n
    \n
  • wt PowerShell.exe -NoExit -Command "& {$Host; $PWD}"失败我不知道如何逃避,但我知道一个解决方法
  • \n
  • wt PowerShell.exe -NoExit -Command "& {$Host, $PWD}"; 尽管这样做仍然是可能的,但我最近找到了一个常规解决方案:使用\\(反斜杠)作为;(分号)itwt参数的转义字符:
  • \n
  • wt PowerShell.exe -NoExit -Command "& {$Host\\; $PWD}"
  • \n
\n

对于更复杂/高级的命令,请在与您的用例类似的用例中应用分组运算符( ),如下所示(从打开的PowerShell会话运行):

\n
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/)}"\n
Run Code Online (Sandbox Code Playgroud)\n

在 Windows 终端中得到以下结果:

\n

Windows 终端

\n

上面的代码将挂起父进程Powershell,直到终端关闭;如果你想继续在父级中Powershell然后使用

\n
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/)}"\n
Run Code Online (Sandbox Code Playgroud)\n

编辑:

\n

Windows 终端使用\\(反斜杠)作为;(分号)的转义字符。因此,我用等效的常规解决方案替换后一种解决方法:

\n
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/}"\n
Run Code Online (Sandbox Code Playgroud)\n

或者,带有-p标志:

\n
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/}"\n
Run Code Online (Sandbox Code Playgroud)\n