PowerShell script to clean up the temporary folder implementation code before the specified number of days _powershell

Source: Internet
Author: User

PowerShell cleans up temporary folders, and when disk space changes, we can resolve them by cleaning up the Temp folder. We use PowerShell to write a program that can clean up files that were 30 days old in the Windows Temp folder.

PowerShell Clean up temporary folders

When disk space changes to an hour, we can resolve it by cleaning up the temporary folder. We use PowerShell to write a program that can clean up files that were 30 days old in the Windows Temp folder.

Copy Code code as follows:

$cutoff = (get-date)-(new-timespan-days 30)
$before = (get-childitem $env: Temp | Measure-object length-sum). Sum

Get-childitem $env: Temp |
Where-object {$_. Length-ne $null} |
Where-object {$_. Lastwritetime-lt $cutoff} |
Remove-item-force-erroraction silentlycontinue-recurse-whatif
$after = (get-childitem $env: Temp | Measure-object length-sum). Sum
$freed = $before-$after
' Cleanup freed {0:0.0} MB. '-F ($freed/1MB)


Description
1. The Windows Temp directory can be expressed in PowerShell using $env:temp.
2, through a $cutoff variable to control the time scale to delete files, 30 days This parameter can be customized.
3, the final calculation of how much space released

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.