Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to have a .bat file that

  • opens a PowerShell console in a new window
  • and runs a .ps1 script
  • and does not exit and close the PowerShell console.

My batch file contains the following line:

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

However, it closes the PowerShell after running the script.

Any advice? Thanks

like image 445
pencilCake Avatar asked Mar 20 '16 10:03

pencilCake


People also ask

How do I run a PowerShell script in a batch file?

If you want to run a few scripts, you can use Set-executionpolicy -ExecutionPolicy Unrestricted and then reset with Set-executionpolicy -ExecutionPolicy Default .

How do I run a PowerShell script without displaying Windows?

If the goal is to start a PowerShell script without a console window, you need to launch powershell.exe from a process that does not itself have a console window. A WSH script launched using wscript.exe does not have a console window, so you can write a WSH script that runs powershell.exe in a hidden window.

How do I run a PowerShell script from a PowerShell script?

To open the PowerShell console, click on the Start button (or search button), type powershell, and click Run as Administrator. To run a script in the PowerShell console, you can either: Use the full path to script, like: C:\TEMP\MyNotepadScript. ps1.


2 Answers

start powershell -noexit -file "D:\MyToolkit\ToolKit.ps1"

Also,

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

like image 67
Avshalom Avatar answered Sep 27 '22 23:09

Avshalom


Not just for the original poster of this question, but for others who might land here looking for answers, the help system is very useful and seems to be often overlooked.

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

You probably could have answered your own 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 exit after running startup commands.

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

like image 32
Kory Gill Avatar answered Sep 27 '22 21:09

Kory Gill