从批处理文件启动的PowerShell窗口

6
我是一位有用的助手,可以为您翻译文本。
我有一个批处理文件,用于启动PowerShell脚本。
批处理文件:
START Powershell -executionpolicy RemoteSigned -noexit -file "MyScript.ps1"

MyScript.ps1 :

Write-Output "Hello World!"

它工作得很好,只有一个例外。窗口的外观类似于旧的cmd.exe(黑色背景),而不是PowerShell(蓝色背景)。如果我从批处理文件启动它,如何获取真正的PowerShell窗口? 谢谢。

2
感谢所有已经发布帖子的人。 我并不真的在乎背景是蓝色还是黑色。我想知道这首先为什么会发生。 如果我这样做: START notepad 那么记事本就会正常启动,并且没有 cmd.exe 的痕迹。记事本看起来完全与我从快捷方式启动它时一样。 为什么PowerShell不同呢?为什么从批处理文件启动它与从快捷方式启动它不同? 而且我有点喜欢在新的 PowerShell 中能够右键粘贴。谢谢。 - mcu
此外,默认情况下,命令提示符只保留300行,而PowerShell保留更多的行。因此,如果您正在使用start powershell -noexit -executionpolicy unrestricted -file "PowerShell script.ps1"之类的东西,则需要在PowerShell中打开它,而不是在命令提示符中,这样您就可以看到脚本的完整历史记录。 - mythofechelon
3个回答

6
如果你真的想要一个蓝色背景,在你的脚本中添加代码以更改背景颜色。
#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

3
这是“开始菜单”中启动PowerShell的shell快捷方式的属性,因此您需要通过它来进行操作:
start "" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk" ...

这不太好看,它有点取决于所在位置(并且可能在外语版本上出现错误)。


我在XP上试了一下,但不起作用。我得到了一个黑色的Shell。start "" "C:\Documents and Settings\All Users\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk"。我必须使用explorer.exe才能尊重LNK属性。 - Andy Arismendi
嗯,我在Windows 7上运行它是可以的。但这就是我试图告诉你的 - 这是一个混乱的事情,很可能会出现故障(特别是在旧操作系统中)。 - Joey

1
你可以调用 PowerShell 以便它用脚本自启动。
Powershell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy RemoteSigned -noexit -File ""Full_Path_of_MyScript.ps1""'}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接