I would like to have a .bat file that
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
If you want to run a few scripts, you can use Set-executionpolicy -ExecutionPolicy Unrestricted and then reset with Set-executionpolicy -ExecutionPolicy Default .
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.
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.
start powershell -noexit -file "D:\MyToolkit\ToolKit.ps1"
Also,
Change the -Command
to -File
as this is what you need
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With