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

My PowerShell script works from the command line, but not from Task_Scheduler. This is PowerShell v1 on Windows Server 2008 R2 Enterprise.

From the command line, I type:

c:\> powershell -File "C:\Program Files\mystuff\foo.ps1"

My one-line PowerShell script runs fine -- it copies the most recent file named *.bak. Here's the script, in case that's useful:

gci X:\backups\*.bak | Sort-Object LastWriteTime -Descending | Select-Object -first 1 | foreach { xcopy $_ z:\backups\ }

I created a scheduled task, with the same command. The script completes instantly with result code 0, but the script didn't actually do anything.

So I tried to see my script's output and changed my scheduled action to:

cmd /c powershell.exe -File "C:\Program Files\mystuff\foo.ps1" > c:\ps.log

Now I see the output: "0 File(s) copied"

Go back to the command line and try it again, and it still works correctly.

The scheduled task is running as the same user as the command line. What is making it behave differently?

share|improve this question
Which version of Windows? – squillman Dec 22 '11 at 15:32
Added version info to description above. – Jesse Dec 22 '11 at 15:54
up vote 2 down vote accepted

I switched from using xcopy to Copy-Item, and got a more informative error message. The problem is that the Z: drive is a mapped network drive -- and scheduled tasks can't see it.

share|improve this answer
Oh sorry I misunderstood your question. Use the UNC instead of a mapped drive. – Ryan Ries Dec 22 '11 at 16:37
Thank you -- the UNC path works. – Jesse Dec 22 '11 at 16:58

In your scheduled task, the action will be just "powershell" and the argument will be ".\script.ps1" or ".\c:\scripts\script.ps1".

If you want to log the activity of said PS script I recommend you do it inside the script itself instead of using the > like you would from within cmd.exe.

Oh sorry I misunderstood your question. Use the UNC instead of a mapped drive.

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.