Powershell专注于新窗口

时间:2014-11-09 11:19:05

标签: powershell powershell-v2.0 powershell-v3.0

我想在从脚本启动的新窗口上运行命令,如下所示:

function start() {

    start-process powershell; echo "test";


}

此命令启动新的PowerShell窗口,但echo "test"在旧窗口中运行,我可以在新窗口中运行命令吗?我正在运行Win7 / 64bit,Powershell v 4.0。

当我使用

function start() {

    echo "test" | start powershell;


}

我收到了这个

start-process : The input object cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At C:\Users\Bane\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:50 char:15
+ echo "test" | start-process powershell;
+               ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (test:PSObject) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.StartProcessCommand

它会打开新窗口,但不会运行命令。

2 个答案:

答案 0 :(得分:2)

使用;实际上是分别运行命令。而是使用-ArgumentList命令选项提供您想要执行的命令或脚本,如下所示

start-process powershell.exe -wait -verb open -ArgumentList "echo Test test";

因此,您的代码应该看起来像

function start() 
{
 start-process powershell.exe -wait -verb open -ArgumentList "echo Test test";
}

答案 1 :(得分:1)

$args = '-noexit -command "echo test"'
Start-Process powershell -ArgumentList $args