我想用参数运行新的 powershell 窗口。我试图运行以下:
powershell -Command "get-date"
Run Code Online (Sandbox Code Playgroud)
但一切都发生在同一个控制台中。有没有一种简单的方法可以做到这一点?
从 PowerShell 打开一个新的 PowerShell 窗口:
Start-Process PowerShell
Run Code Online (Sandbox Code Playgroud)
或者我个人最喜欢的:
Start-Process PowerShell -WindowStyle Maximized
Run Code Online (Sandbox Code Playgroud)
然后,您Get-Date无需处理 -ArgumentList 自行关闭的趋势即可键入。我仍然没有找到一种方法来使用 -ArgumentList 参数打开一个新的 PowerShell 进程,而不是在它运行后立即关闭。例如:
Start-Process PowerShell -ArgumentList "Get-Date"
Run Code Online (Sandbox Code Playgroud)
或者干脆
Start-Process PowerShell -ArgumentList Get-Date
Run Code Online (Sandbox Code Playgroud)
运行该过程后将立即关闭。
为了让它在关闭之前等待,您可以添加:
Start-Process PowerShell -ArgumentList 'Get-Date; Read-Host "Press Enter"'
Run Code Online (Sandbox Code Playgroud)
由于 -Wait 参数在这种情况下似乎什么都不做。
仅供参考 - PowerShell 建议的语法实际上是:
Start-Process -FilePath "powershell.exe"
Run Code Online (Sandbox Code Playgroud)
但是由于 PowerShell 是 %SystemRoot%\system32 环境变量中的标准 Windows 应用程序,因此命令行应该识别一个简单的
Powershell
Run Code Online (Sandbox Code Playgroud)
命令
使用start命令。在 CMD 提示中,尝试:
start powershell -noexit -command "get-date"
对于开始/运行(或 Win+r)提示,请尝试:
cmd /c start powershell -noexit -command "get-date"
该-noexit会告诉PowerShell来,好了,不退出。如果省略此参数,将执行该命令,您可能只会看到 Powershell 窗口的一瞥。对于交互式使用,这是必须的。对于脚本,它不是必需的。
编辑:
start是 CMD 的内部命令。在 Powershell 中,它是Start-Process. 这些不是一回事。
至于为什么窗口是黑色的,那是因为Powershell.exe的快捷方式被配置为背景蓝色。
| 归档时间: |
|
| 查看次数: |
9271 次 |
| 最近记录: |