replacing a scheduled task with powershell

rated by 0 users
This post has 3 Replies | 1 Follower

Top 150 Contributor
Posts 13
kanoute696 Posted: 03-28-2012 11:04 AM

Is it possible to run a powershell script and tell it to run once a day or hour indefinitely enabling me to not use scheduled task. 

It s to copy a file each day. This is my copy script below - its pretty basic as I am beginner. 

 

Thanks 

$source = “\\xxxx\d$\data\home”

$destination1  = “\\xxxx\d$\logs\location1"

$destination2  = “\\xxxx\d$\logs\location2"

$files = Get-ChildItem -Filter *.* -Path $source -Recurse

Foreach($file in $files)

{

Copy-Item -Path $file.fullname -Destination $destination1  -verbose -PassThru >> c:\scripts\logs\test.log

Copy-Item -Path $file.fullname -Destination $destination2  -verbose -PassThru >> c:\scripts\logs\test.log

Top 10 Contributor
Posts 598
Microsoft MVP
Top Contributor

you would schedule your powershell script with a scheduled task.

When you schedule the script, make sure you specify "powershell.exe" as application, and submit these as arguments:

-noprofile -executionpolicy bypass -file "c:\pathtoscript.ps1"

 

 

Top 150 Contributor
Posts 13

Thanks for coming back..

so there is nothing in powershell that I could replace a scheduled task with. Could I not get it to loop every hour\day? or something like this?. Thank you

Top 10 Contributor
Posts 598
Microsoft MVP
Top Contributor

yes you can:

 

$hour = 60 * 60

do {

# do something

Start-Sleep -second $hour

} while ($true)

 

However, for this to work you would first have to launch the script manually, and depending on how robust your solution needs to be, make sure no one closes the powershell session that runs the script.

For your personal use or on your own machine, this may be a doable way.

Page 1 of 1 (4 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.