【问题标题】:Set window title when running "start powershell" in powershell not working?在powershell中运行“start powershell”时设置窗口标题不起作用?
【发布时间】:2019-12-05 16:25:22
【问题描述】:

以下命令适用于CMD (How to start powershell with a window title?)。

start powershell -NoExit -command "$Host.UI.RawUI.WindowTitle = 'bits'"

但它在 Powershell 中不起作用。

PS C:\> 启动 powershell -noexit -command "$Host.UI.RawUI.WindowTitle = 'test'; 读取主机" 启动进程:找不到与参数名称“noexit”匹配的参数。 在行:1 字符:18 + 启动 powershell -noexit -command "$Host.UI.RawUI.WindowTitle = 'test ... + ~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

以下命令可以打开一个新的powershell窗口。

start powershell "$Host.UI.RawUI.WindowTitle = 'test'; read-host"

但是,新窗口显示以下错误消息并且标题未设置。

System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.WindowTitle :术语 'System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.WindowTitle' 未被识别为 cmdlet、函数、脚本文件或可运行的程序。检查名称的拼写,或者如果包含路径,请验证 路径正确,然后重试。 在行:1 字符:1 + System.Management.Automation.Internal.Host.InternalHost.UI.RawUI.Wind ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.Manageme...wUI.WindowTitle:String) [], CommandNotFoundException +fullyQualifiedErrorId:CommandNotFoundException

【问题讨论】:

  • start powershell '-NoExit -command "$Host.UI.RawUI.WindowTitle = ''bits''"'

标签: powershell


【解决方案1】:

Bacon Bits' helpful answer 解释说,start 中的 cmd.exe 与 PowerShell 中的含义不同。

如下使用Start-Process得到想要的结果;请注意,powershell 隐式绑定到参数 -FilePath,而以 -NoExit 开头的 , 分隔参数隐式绑定到 -ArgumentList (-Args) 参数,该参数接受 array 字符串:

# In PowerShell, `start` is an alias for `Start-Process`
start powershell '-NoExit', '-command', "`$Host.UI.RawUI.WindowTitle = 'bits'"

特别是,--prefixed pass-through 参数必须引用,以免它们被误认为Start-Process 自己的参数。 p>

还要注意$Host 中$ 之前的`,这可以防止调用 PowerShell 实例预先 插入$Host。

您也可以使用'$Host.UI.RawUI.WindowTitle = ''bits''',一个单引号文字字符串,其中嵌入单引号转义为''。


重要:

虽然将参数作为数组传递给-ArgumentList是概念上最好的方法,但不幸的是,由于长期存在的错误,这是不明智的Start-Process,在撰写本文时仍然存在 (v7.1) - 请参阅 GitHub issue #5576。

目前,使用包含 all 参数的 单个 字符串,并根据需要包含在 embedded "..." 引用中,是只有一般稳健的方法。正如链接的 GitHub 问题中所讨论的,将来可能会引入支持基于数组的稳健参数传递的 -ArgumentArray 参数。

正如PetSerAl 在对该问题的评论中所建议的那样,这意味着以下情况:

Start-Process powershell '-NoExit -command "$Host.UI.RawUI.WindowTitle = ''bits''"'
  • 注意整个参数列表字符串的 single-quoting_ ('...'),然后需要转义 embedded 单引号 - PowerShell 应该将其视为命令的一部分 - 作为''。

【讨论】:

    【解决方案2】:

    在命令提示符中,start 是 the start internal command。在 Windows Powershell 中,start 是 Start-Process 的别名,其作用类似但不相同。

    尝试运行这个:

    powershell -NoExit -command "`$Host.UI.RawUI.WindowTitle = 'bits'"
    

    【讨论】:

    • 它不会弹出一个新窗口。我想弹出一个新窗口。 (我实际上需要并行运行多个窗口。)
    【解决方案3】:

    这是另一种方式,同时避免使用美元符号。双引号必须在外面,以便位保持引用。

    start powershell "-noexit (get-variable host).value.ui.rawui.windowtitle = 'bits'"
    

    将命令放入文件中,您始终可以避免这些引用问题。

    start powershell '-noexit .\window.ps1'
    

    【讨论】:

    • 或者,更短的:(Get-Host).
    • 默认动词逻辑很有吸引力,但晦涩难懂,如您的示例所示。此外,它的性能很差并且没有完全集成 - 请参阅 github.com/PowerShell/PowerShell/issues/3987
    【解决方案4】:

    使用设置窗口标题的命令选项启动 powershell 对我不起作用。也许是因为我想用 ps1 文件打开另一个 powershell。所以在另一个 ps1 文件中我添加了第一行如下,

    (Get-Host).ui.RawUI.WindowTitle='TEST TEST'

    它就像一个魅力......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-09
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2012-04-08
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多