2012-02-16 83 views
6

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

批处理文件:

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

MyScript.ps1:

Write-Output "Hello World!" 

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

谢谢。

+2

感谢所有到目前为止谁已经张贴你可以调用的PowerShell。 我不在乎背景是蓝色还是黑色。我想知道为什么这首先发生。 如果我这样做: 启动记事本 然后记事本可以正常启动,也没有围绕CMD.EXE的痕迹。记事本看起来完全一样,如果我从快捷方式启动它。 为什么它与PowerShell不同?为什么它从批处理文件和快捷方式启动它不同? 而且我有点像能够在新的PowerShell中右键单击粘贴。 谢谢。 – mcu 2012-02-18 02:47:32

+0

另外,默认情况下,命令提示符只保留300行,而PowerShell保留更多行,所以如果您使用类似'start powershell -noexit -executionpolicy unrestricted -file“PowerShell script.ps1”'那么您需要它实际打开在PowerShell中,不是命令提示符,因此您可以查看脚本的完整历史记录。 – mythofechelon 2016-05-25 14:42:37

回答

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 
+0

+1。 :)不错(也很快)。 – 2012-02-17 18:50:51

3

这是在启动PowerShell中的开始菜单的外壳链接的属性,所以你必须要经过:

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

它不漂亮,这取决于在那里驻留了一点(和可能会打破外语版本)。

+0

我在XP上试过了,没有用。我有一个黑色的外壳。 '开始“”“”C:\ Documents and Settings \ All Users \ Start Menu \ Programs \附件\ Windows PowerShell \ Windows PowerShell.lnk“'。我不得不使用'explorer.exe'来承认LNK属性。 – 2012-02-16 19:12:23

+0

那么,我在Windows 7上,它在这里工作。但那正是我想告诉你的 - 这是一件混乱的事情,可能会崩溃(特别是在传统操作系统中)。 – Joey 2012-02-17 07:31:47

1

,使其启动自身用脚本

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