Forum

Powershell, cleanup...
 
Notifications
Clear all

Powershell, cleanup, delete old files, task scheduler

1 Posty
1 Users
0 Reactions
847 Widok
0
Topic starter

example script

1 Answer
0
Topic starter

1. Cleanup.ps1

$Folder = "C:ToBackup"
$Daysback = "-7"

Add-Content C:logsdelete_log.txt   "Cleanup started $((Get-Date).ToString())"

Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays($Daysback)} |
ForEach-Object {
   $_ | del -Force
   $_.FullName | Out-File C:logsdelete_log.txt  -Append
}

#Delete empty folders and subfolders
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {$_.PsIsContainer -eq $True} |
? {$_.getfiles().count -eq 0} |
ForEach-Object {
    $_ | del -Force
    $_.FullName | Out-File C:logsdelete_log.txt  -Append
}

2. Task Scheulder

C:WindowsSystem32WindowsPowerShellv1.0powershell.exe -ExecutionPolicy Bypass –NoProfile –Command "& {Cleanup.ps1; exit $LastExitCode}" > C:logs.log

Odpowiedź

Author Name

Author Email

Your question *

Preview 0 Revisions Saved
Share: