【问题标题】:PowerShell window when started from a batch file从批处理文件启动时的 PowerShell 窗口
【发布时间】:2012-02-16 18:50:05
【问题描述】:

我有一个启动 PowerShell 脚本的批处理文件。

批处理文件:

START Powershell -executionpolicy RemoteSigned -noexit -file "MyScript.ps1"

MyScript.ps1:

Write-Output "Hello World!"

它工作正常,但有一个例外。窗口的外观就像旧的 cmd.exe(黑色背景)而不是 PowerShell(蓝色背景)。
如果我从批处理文件启动它,如何获得真正的 PowerShell 窗口?

谢谢。

【问题讨论】:

  • 感谢到目前为止所有发帖的人。我真的不在乎背景是蓝色还是黑色。我想知道为什么会发生这种情况。如果我这样做:启动记事本然后记事本正常启动并且周围没有 cmd.exe 的痕迹。记事本看起来就像我从快捷方式启动它一样。为什么它与 PowerShell 不同?如果我从批处理文件与从快捷方式启动它,为什么会有所不同?我有点喜欢在新的 PowerShell 中右键单击进行粘贴。谢谢。
  • 此外,默认情况下,命令提示符仅保留 300 行,而 PowerShell 保留更多行,因此如果您使用 start powershell -noexit -executionpolicy unrestricted -file "PowerShell script.ps1" 之类的内容,则需要在 PowerShell 中实际打开它,而不是命令提示符,这样您就可以看到脚本的完整历史记录。

标签: powershell command-line batch-file


【解决方案1】:

如果您真的想要蓝色背景,请在您的脚本中添加代码以更改背景颜色。

#save the original
$original=$host.ui.RawUI.BackgroundColor
$front=$host.ui.RawUI.ForegroundColor
$host.ui.RawUI.BackgroundColor="DarkBlue"
$host.ui.RawUI.ForegroundColor="White"
cls
#run your code
dir c:\scripts

#set it back
$host.ui.RawUI.BackgroundColor=$original
$host.ui.RawUI.ForegroundColor=$front

【讨论】:

    【解决方案2】:

    这是启动 PowerShell 的“开始”菜单中 shell 链接的一个属性,因此您必须执行该操作:

    start "" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk" ...
    

    它并不漂亮,它在一定程度上取决于它所在的位置(并且可能会因外语版本而中断)。

    【讨论】:

    • 我在 XP 上试过了,没有用。我有一个黑色的外壳。 start "" "C:\Documents and Settings\All Users\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk"。我必须使用 explorer.exe 来尊重 LNK 属性。
    • 嗯,我在 Windows 7 上,它在这里工作。但这就是我想告诉你的——这是一件混乱的事情,很可能会崩溃(尤其是在旧版操作系统中)。
    【解决方案3】:

    您可以调用 powershell 以便它使用脚本自行启动

    Powershell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy RemoteSigned -noexit -File ""Full_Path_of_MyScript.ps1""'}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2021-12-19
      • 2013-09-20
      相关资源
      最近更新 更多