Hi all,
I have found a lot about that topic but nothing was solving my Problem maybe you can help me. The Target of the script is to clean up a Public share drive with Files they are older then 2day's by CreationTime. After it checks also for empty folder and delete it as well.
That was the "easy" part for me. Now to have a controlling on the job i would like to have a log File. The Schedule Task once per day. The Script generate a logfile with the name of the date.
The Issue what i'm facing now, is to get a soloution to count all the failed / successfully deleting of files. I would like to see how many went well and how many he could not delete maybe beceause the File was corrupt or there was a session open o the file.
# all Variable $limit = (Get-Date).AddDays(-2) $count = 0 $FilesFailed = 0 $FilesSuccessfully = 0 $Files = Get-ChildItem $path -Name $Totalfiles = Get-ChildItem $path -Recurse $path = "C:\temp\ScriptTest" # Delete files older than 2Day's Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | ForEach-Object{$count++} | Remove-Item -Force -WhatIf $TotalFilesLog = +$TotalFiles.Count # Write footer to log and output to console Write-Output ($Logfile = @" $("-"*79) Total Files : $TotalFileslog Files to delete : $Count Files Failed : $FilesFailed Files Successfully : $FilesSuccessfully Finished Time : $EndDate $("-"*79)"@) | Out-File -FilePath "C:\temp\$(get-date -f yyyy-MM-dd).txt"
# Delete any empty directories left behind after deleting the old files Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse -WhatIf
Thanks a lot for help or clous to get it done.
Regards,
Ari