By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,708 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,708 IT Pros & Developers. It's quick & easy.

Can Someone Assist With Modifying a Simple PowerShell Script?

MCMike
P: 11
Hello Team,

I have a couple of simple PowerShell scripts that delete files older than 1 day from various folders (hard drive and UNC).

However, I am looking to modify them to run at a set time in the day so I don't have to trigger them manually.

Can someone assist with this? I have included the code from one of them. If this assistance is not available, thank you anyway!

Expand|Select|Wrap|Line Numbers
  1. #----- define parameters ---#
  2. #----- get current date ---#
  3. $Now = Get-Date
  4. #----- define amount of days ---#
  5. $Days = "1"
  6. #----- define folder where files are located ---#
  7. $TargetFolder = "C:\Backups\Temp\Daily Backups"
  8. #----- define Extension ---#
  9. $Extension = "*.zip"
  10. #----- define LastWriteTime parameter based on $Days ---#
  11. $LastWrite = $Now.Adddays(-$Days)
  12.  
  13. #----- get files based on lastwrite filter and specified folder ---#
  14. $Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
  15.  
  16.  
  17. foreach ($File in $Files)
  18.     {
  19.     if ($File -ne $Null)
  20.         {
  21.         write-host "Deleting File $File" -ForegroundColor "Red"
  22.         Remove-Item $File.FullName | out-null
  23.         }
  24.         else
  25.         {
  26.         Write-Host "No more files to delete!" -foregroundcolor "Green"
  27.         }
  28.     }
Cheers,

MCMike
Jun 15 '17 #1
Share this Question
Share on Google+
1 Reply


MCMike
P: 11
Team,

Stand down on this request. I am pleasantly surprised that I am able to utilize the Task Scheduler. Most functions are administrator only on our tight-ass network, but for some reason there is no restriction with PowerShell and the Scheduler!

Thanks.

MCMike
Jun 16 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.