如何从.bat在新窗口中启动PowerShell并在不退出控制台的情况下运行.ps1?

pen*_*ake 12 powershell batch-file

我想有一个.bat文件

  • 在新窗口中打开PowerShell控制台
  • 并运行.ps1脚本
  • 并且不会退出并关闭PowerShell控制台.

我的批处理文件包含以下行:

start powershell.exe -Command "&'D:\MyToolkit\ToolKit.ps1'"
Run Code Online (Sandbox Code Playgroud)

但是,它在运行脚本后关闭PowerShell.

有什么建议?谢谢

Avs*_*lom 14

start powershell -noexit -file "D:\MyToolkit\ToolKit.ps1"
Run Code Online (Sandbox Code Playgroud)

也,

更改为-Command,-File因为这是您需要的


Kor*_*ill 5

不仅对于这个问题的原始发布者,而且对于其他可能来这里寻找答案的人来说,帮助系统非常有用,但似乎经常被忽视。

使用命令 /? 或和PowerShell命令都很有用。get-helpget-help -full

在这种情况下,您可能可以通过阅读您想要运行的命令的帮助来回答您自己的问题powershell。

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

-NoExit
    Does not exit after running startup commands.

...

-File
    Runs the specified script in the local scope ("dot-sourced"), so that the
    functions and variables that the script creates are available in the
    current session. Enter the script file path and any parameters.
    File must be the last parameter in the command, because all characters
    typed after the File parameter name are interpreted
    as the script file path followed by the script parameters.
Run Code Online (Sandbox Code Playgroud)

-NoExit -- 运行启动命令后不退出。

-File -- ...文件必须是命令中的最后一个参数...