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

从批处理文件启动时的PowerShell窗口,powershell,command-line,batch-file,Powershell,Command Line,Batch File,我有一个启动PowerShell脚本的批处理文件 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyScript.ps1" MyScript.ps1: Write-Output "Hello World!" 它工作正常,只有一个例外。窗口的外观类似于旧的cmd.exe(黑色背景),而不是PowerShell(蓝色背景)。 如果从批处理文件启动,如何获得真正的PowerShell窗口 谢谢。这是启动Po

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

批处理文件:

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

Write-Output "Hello World!"
它工作正常,只有一个例外。窗口的外观类似于旧的cmd.exe(黑色背景),而不是PowerShell(蓝色背景)。
如果从批处理文件启动,如何获得真正的PowerShell窗口


谢谢。

这是启动PowerShell的“开始”菜单中shell链接的属性,因此您必须完成以下操作:

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

这并不漂亮,这有点取决于它所在的位置(在外语版本中可能会中断)。

如果您确实需要蓝色背景,请在脚本中添加代码以更改背景颜色

#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

您可以调用powershell,使其以脚本启动

Powershell.exe-Command“&{Start Process Powershell.exe-ArgumentList'-ExecutionPolicy RemoteSigned-noexit-File”“完整的\u路径\u的\u MyScript.ps1”“}”

我在XP上试用过,但没有效果。我有一个黑色的外壳<代码>开始“”“C:\Documents and Settings\All Users\start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk”。我必须使用
explorer.exe
来尊重LNK属性。我在Windows7上,它在这里工作。但这正是我想告诉你们的——这是一件混乱的事情,很可能会崩溃(特别是在遗留操作系统中)。感谢所有到目前为止发表文章的人。我真的不在乎背景是蓝色还是黑色。我想知道为什么会发生这种事。如果我这样做:启动记事本,则记事本会正常启动,周围没有cmd.exe的痕迹。记事本看起来就像是从快捷方式开始的。为什么与PowerShell不同?为什么从批处理文件启动与从快捷方式启动不同?我有点喜欢在新的PowerShell中通过右键单击进行粘贴。谢谢。另外,默认情况下,命令提示符仅保留300行,而PowerShell保留更多行,因此如果您使用类似于
start PowerShell-noexit-executionpolicy unrestricted-file“PowerShell script.ps1”
的内容,则需要在PowerShell中实际打开它,而不是在命令提示符中打开它,因此,您可以查看脚本的完整历史记录。