25

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

cmd.exe /k mybatchfile.bat

3 Answers 3

34

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".

Sign up to request clarification or add additional context in comments.

Comments

22

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.

Comments

-2

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"

1 Comment

Can be test.bat another ps1 script (c:\path to a script\test.ps1) ? any full sample about it ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.