How do I make Powershell run a batch file and then stay open?


For example; with the old command prompt it would be:

cmd.exe /k mybatchfile.bat


When running PowerShell.exe just provide the -NoExit switch like so:

PowerShell -NoExit -File "C:\SomeFolder\SomePowerShellScript.ps1"

PowerShell -NoExit -Command "Write-Host 'This window will stay open.'"

Or if you want to run a file and then run a command and have the window stay open, you can do something like this:

PowerShell -NoExit "& 'C:\SomeFolder\SomePowerShellScript.ps1'; Write-Host 'This window will stay open.'"

The -Command parameter is implied if not provided, and here we use the & to call the PowerShell script, and the ; separates the PowerShell commands.

Also, at the bottom of my blog post I show a quick registry change you can make in order to always have PowerShell remain open after executing a script/command, so that you don't need to always explicitly provide the -NoExit switch all the time.

By : deadlydog

I am sure that you already figure this out but I just post it

$CreateDate = (Get-Date -format 'yyyy-MM-dd hh-mm-ss')

$RemoteServerName ="server name"
$process = [WMICLASS]"\\$RemoteServerName\ROOT\CIMV2:win32_process"  
$result = $process.Create("C:\path to a script\test.bat") 
$result | out-file -file "C:\some path \Log-$CreatedDate.txt"
By : john

Drop into a cmd instance (or indeed PowerShell itself) and type this:

powershell -?

You'll see that powershell.exe has a "-noexit" parameter which tells it not to exit after executing a "startup command".

This video can help you solving your question :)
By: admin