Reputation: 141
I develop a PowerShell application. I use for example
Start-Process "cmd.exe" "/c some_command"
#rest of the source code
But when I run this, it is not waiting for cmd.exe
to close before executing the rest of the source code.
How can I get cmd.exe
"exit event" return to continue to run my PowerShell script?
Or other way to sleep PowerShell script while cmd.exe is running?
Upvotes: 0
Views: 368
Reputation: 1059
In addition to using -Wait
, you could also check the exit code to verify command success:
[int]$ExitCode = (Start-Process "cmd.exe" "/c some_command" -Wait).ExitCode
# Check exit code
if ($ExitCode -ne 0) {
throw "Something went wrong.
}
This is useful for debugging and checking cmd
success.
Upvotes: 1
Reputation: 200193
You can invoke CMD directly from PowerShell:
cmd.exe /c some_command
In case the executable is provided as a string rather than a bare word you must prefix the statement with the call operator (otherwise using the operator is optional):
& 'cmd.exe' /c some_command
This kind of invocation automatically runs synchronously.
Start-Process
invocation is asynchronous by default. You can change the behaviour to synchronous by adding the parameter -Wait
.
Upvotes: 1
Reputation: 141
I found the answer is to use -wait
Start-Process "cmd.exe" "/c some_command" -wait
Upvotes: 1