1

I am trying to execute a very simple PowerShell script from Windows Scheduled Tasks.

My PowerShell script is this:

$currentTime = (Get-Date).ToString('yyyy-MM-ddTHH.mm.ss')
$contentToDump = "JLS"
$path = "$home\Desktop\resutfile_" + $currentTime + ".txt"
Add-Content $path $contentToDump

So it simply writes the string "JLS" to a file on my desktop with the current time as part of the name.

If I execute it using Windows PowerShell ISE it works fine. It also works fine when executing using the command prompt.

But when I hook up at Scheduled task it doesn't work. The task returns with a valid ("0x0")-result but no file is generated.

My configuration of the task is this:

Action: Start a program
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command “&{c:\Users\jls\desktop\untitled2.ps1}”
Start in (optional): C:\users\jls\desktop

The task is set to run as "me" with highest privileges and it is running locally on my machine where I am admin.

What am I missing?

3
  • Try using the File param instead of Command in your Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "c:\Users\jls\desktop\untitled2.ps1" Commented Mar 29, 2017 at 11:19
  • For debug, try to put full path, like $path='C:\users\jls\desktop\resultfile_'+... Commented Mar 29, 2017 at 11:38
  • And put to your script line: add-content 'c:\users\public\homevalue.txt' $home and check it content after execution Commented Mar 29, 2017 at 11:41

1 Answer 1

4

You need to replace the -Command with -File in your Add Arguments

Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File c:\Users\jls\desktop\untitled2.ps1

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