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

I have a couple of Exchange 2010 PowerShell scripts that I'd like to run as Scheduled Tasks.

If I launch PowerShell using "Run as different user" I can run the scripts and they execute correctly.

If I schedule a task using that same user, the task stays in the Running state forever.

How can I figure out where the task is getting stuck?

For reference, here's how I enable the Exchange stuff:

. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto

And here is the ways I have tried to run the script from the Task Scheduler:

  • powershell -command "& {. 'c:\windows\script.ps1' }"
  • powershell -file 'c:\windows\script.ps1'
  • powershell -file "c:\windows\script.ps1"

All with the same result. Grr...

share|improve this question

5 Answers 5

Not sure if this is how you are doing it, but when I am running PowerShell scripts via task scheduler I use the "Start a program" action and select powershell and then add the arguments from there. That might be what you are doing here, but it's kind of unclear. Here's a screenshot: enter image description here

Per TheCompWiz execution policy might also be an issue.

share|improve this answer
That's not even valid command line syntax for powershell.exe, but just for the heck of it I tried it. The task immediately failed. – longneck Nov 1 '12 at 18:20
Interesting because all of my daily powershell tasks run that way and they function just fine. – ZnewmaN Nov 1 '12 at 20:21
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -command "& 'c:\windows\script.ps1' " – ZnewmaN Nov 1 '12 at 20:24
ah, there's the difference. your screenshot doesn't show the -command – longneck Nov 1 '12 at 20:44
Yeah, I actually personally don't use -command, but it is probably the proper use. – ZnewmaN Nov 1 '12 at 20:52

You either need to modify your execution policy, or specify the -ExecutionPolicy Bypass as a commandline parameter.

powershell -Command "<path to .ps1 script>" -ExecutionPolicy Bypass
share|improve this answer
I added the ExecutionPolicy option to my command line parameters. No change; task still runs forever. – longneck Nov 1 '12 at 18:20

I had the same problem. In my case the solution was to specify the "start in" directory (my script would read the contents of a file which it wouldn't find because I didn't specify the full path to the file).

share|improve this answer

First of all, try to run it like this

powershell -ExecutionPolicy Bypass -command "& 'FilePathToScript.ps1'"

Second, do you Import a PSSession in your Script (usually you do that in ExchangeRemoteShell)? if yes, try to End the pssession in the end of your script, with one of the following cmdlets Disconnect-PSSession if that doesn't work try Remove-PSSession or Exit-PSSession

and put an exit command at the end of your script.

with those things, it should work

EDIT: Just saw that you don't connect to remote. is there an opposite command to connect-exchangeserver? something like disconnect-exchangeserver?

share|improve this answer

Try add to the end of your script [taskkill /f /im "powershell.exe"] this will kill all "powershell.exe" processes. I don't know how to kill the current power-shell process (equivalent to [cmd.exe /c]). But it will do the trick.

share|improve this answer
And what if there were another powershell scripts running, probably doing something important? Would you just kill these processes without asking? On a production environment? As a routine? Seriously?! – Esa Jokinen Apr 1 at 8:10

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.