Last active
September 5, 2015 15:30
-
-
Save Dan1el42/23b3901cae06d86d44f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Targetfolder ="C:\temp\logs" | |
$Extension = "*.log" | |
$logfile = New-Item -Path 'C:\TEMP' -ItemType "file" -Name deleted.log | |
$files = Get-ChildItem $Targetfolder -Include $Extension -Recurse | where {$_.LastWriteTime -lt (get-date).Adddays(-180)} | |
foreach ($File in $Files) | |
{ | |
if ($File -ne $Null) | |
{ | |
write-host "Deleting File $File" -BackgroundColor DarkGreen | |
Remove-item $File.Fullname -Force | |
# Remove-Item does not return the $File object back to the pipeline. | |
# You need to include the Out-File within the foreach loop where you have access to the $File object. | |
$File.FullName | Out-File -FilePath $logfile -Append | |
} | |
else { | |
write-host "No more files to delete" -ForegroundColor DarkRed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment