Link to home
Start Free TrialLog in
Avatar of IM&T SRFT
IM&T SRFT

asked on

Powershell script not running when set as a Scheduled Task

I have created a script to take the files from one directory and copy them to others before deleting the original

The script runs fine in PowerShell  locally but when putting as a scheduled task, the task scheduler is showing 'Task Completed' but the original file remains where it is and does not get copied.

Run Command Line - powershell -file "C:\Desktop\Demographic (Spine) Feed.ps1"
I have chosen to run with highest priviledges too

# Copy everything under C:\Folder1 to C:\Folder2 & C:\Folder3
get-childitem "C:\Folder1" | % { 
    copy-item $_.FullName -destination "C:\Folder2\$_" -recurse
    copy-item $_.FullName -destination "C:\Folder3\$_" -recurse
}

# Remove everything under C:\Folder1
get-childitem "C:\Folder1" -recurse | % { 
    remove-item $_.FullName -recurse 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

Shaun,

He already has the path quoted, in which case the parens and spaces in the file name pose no problem.

~bp
Bill,

It is an issue is you do not use -file so safer to not use them. I see Michael also recommended -file
I just saw OP is using -file too
User generated image
give a try with adding this line on top of your code
Set-ExecutionPolicy Bypass

Open in new window

Avatar of IM&T SRFT

ASKER

Thank you all very much

I deleted the task entirely and started again with the following changes and it now works

Bill - Thank you for your contribution throughout

Muhammad - I used that link before hand but still managed to get it wrong!

Michael - I split the command line  and typed powershell.exe in full

Shaun - I initially removed the brackets in the name of the file and it still did not work but when I created the task again I removed all of the references to the brackets from the task including the task name and description and coinciding with the other changes this is where it all started to work again