Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The script is fairly straight forward. Simply tries to start a bunch of windows services. Execution locally works fine when on the target machine. The script is actually executing fine as well when done via PsExec, it just never returns until I hit the "enter" key on my CMD prompt. This is a problem, because this is being called from TeamCity, and it makes the Agent hang waiting for PsExec to return.

I've tried the following:

  • Adding an exit and exit 0 at the end of the Powershell script
  • Adding a < NUL to the end of the PsExec call, per the answer in this SF question
  • Adding a > stdout redirect

This is how I am actually calling psexec:

psexec \\target -u domain\username -p password powershell c:\path\script.ps1

No matter what I do, it hangs until I the locally on the cmd prompt. After I hit enter, I get the message:

powershell exited on target with error code 0.
share|improve this question
up vote 9 down vote accepted

Turns out this is a common problem. Found the solution a here. Essentially, if you pipe some data on stdin with cmd it will return propertly after execution (because it is being run via cmd, not powershell).


psexec \\target -u domain\username -p password cmd /c "echo . | powershell c:\path\script.ps1"
share|improve this answer
is it set up as Command Line - Custom Script or Command Line - Executable (in TeamCity)? – Turowicz Jul 10 '13 at 10:36
OK Ive set it up as Executable and it helped. Thanks mate I've spent 1 day diagnosing it. Spread the word! – Turowicz Jul 10 '13 at 10:45
After a day the problem returned... – Turowicz Jul 11 '13 at 9:17

I know the answer comes late it would have already been figured out, If not it might be useful for future visitors.

STDIN has to be re-directed in powershell execution inorder to be able to come from hang (Here it waits in STDIN). to be able to do this use -inputformat none

powershell -inputformat none -File powershell_script.ps1 will work.

Check -

share|improve this answer
psexec \\target -u domain\username -p password -d powershell c:\path\script.ps1

Also fixes the problem.

The -d flag for psexec is like "run and exit" in a non-interactive way:

-d Don’t wait for the application to terminate.

Only use for non-interactive applications.

share|improve this answer

Powershell is not exiting. Try this command line

Powershell -command script.ps1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.