How to start Powershell in a new window from .Bat and run .ps1 without exiting the console?

I would like to have a .bat file which

  • Open the Powershell console in a new window
  • And to run the .ps1 script
  • And does not exit or close Powershell

My bat file has the following line:

start powershell.exe -Command "&'D:\MyToolkit\ToolKit.ps1'" 

However, it will close powershell after running the script.

Can I advise? Thanks

+5
source share
2 answers
 start powershell -noexit -file "D:\MyToolkit\ToolKit.ps1" 

Besides,

Change -Command to -Command as this is what you need

+8
source

Not only for the original poster of this question, but also for others who may land here to search for answers, the help system is very useful and seems to be often overlooked.

Using the /? or in PowerShell the get-help and get-help -full are useful.

You could probably answer your question by reading the help for the command you wanted to run PowerShell in this case.

 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. 

-NoExit - does not start after running start commands.

-File -... The file must be the last parameter in the command ...

+2
source

Source: https://habr.com/ru/post/1245415/


All Articles