Disk Management and Auto Cleanup

PowerShell Related Scripting Forum
User avatar
hamid
Site Admin
Posts: 17
Joined: Mon Jun 17, 2019 2:19 pm
Contact:

Disk Management and Auto Cleanup

Postby hamid Tue Sep 22, 2020 1:48 pm

Hello Club,

In this post I'm gonna share a script which will automate your disk cache cleanup process and will ease the way for increasing the disk size before attempting to increase it in hypervisor or physically.

Here we go.

This script first will show you the current space and free space of system drive and then will remove inactive users older than 1 year, then will clear inetpub folder for IIS with the logs older than 14 days, also will cleanup temp folder, Perflogs and WER\ReportQueue in ProgramData. Previously, I had the script for running DISM commands for cleaning up WinSxS folder however, then I removed it from the script, if you need it just send me an email and I will prepare it for you :)

### Check Disk Space and Clean Up Cache ##
##########################################
#
#
# First Show Disk #
# Cleanup mentioned locations #
# Show Disk Again #
# Save the Report #
#
#
##########################################


# Define Variables with Folder Locations

param(

[string]$Temp = "C:\Windows\temp",
[string]$PerfLogs = "C:\PerfLogs",
[string]$ProgData = "C:\ProgramData\Microsoft\Windows\WER\ReportQueue",
[string]$All = @($temp,$PerfLogs,$ProgData),
[string]$InetPub = "C:\inetpub\logs\LogFiles\W3SVC1",
[string]$Users = "C:\users",
[string]$WinSXS = "C:\Windows\WinSxS",
[string]$FileName = "DiskCleanUp_LOG-" + (Get-Date -f yyyy-MM-dd_HH-mm-ss),
$Days = (Get-Date).AddDays(-14),
$Years = (Get-Date).AddYears(-1)

)


#Define Function DiskCleanup

Function Run-DiskCleanUp

{

# Show Current Disk Size and Free Space

Write-Host "Before Cleanup Space" -ForegroundColor Cyan

$ShowDisk = Get-WmiObject win32_logicaldisk |
Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

$ShowDisk

# Show Inetpub Items and clear the logs which are older than 14 Days

$InetPubOld = Get-ChildItem -Path $InetPub -Recurse -Force |
Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Days } | Remove-Item -Force

# Show users and Delete the profile which were inactive since 1 Year ago

$UsersOld = Get-ChildItem -Path $Users -Force |
Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt $Years } | Remove-Item -Force

$NewFile = New-Item -Path C:\ -Name $FileName -ItemType File


# Remove Action


if ($Temp -and $PerfLogs -and $ProgData -ne $NULL)

{
write-host "Deleting Files..." -ForegroundColor "Red"
Remove-Item $Temp -Force -EA SilentlyContinue | Out-String
Remove-Item $PerfLogs -Recurse -Force -EA SilentlyContinue
Remove-Item $ProgData -Recurse -Force -EA SilentlyContinue

}

else

{

Write-Host "No more files to delete!" -foregroundcolor "Green"

}


# Show Final Disk Space

Write-Host "After Cleanup Space" -ForegroundColor Cyan
$ShowDisk

}


Run-DiskCleanUp



Hope it'll help you :)
Microsoft MVP: Cloud and Datacenter Management
Info@hamidsadeghpour.net

Return to “General Scripting”

Who is online

Users browsing this forum: No registered users and 1 guest