Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
TheServerGeeks

IT with everything IT

[How To]Disk Cleanup using PowerShell

/

Disk Cleanup (cleanmgr.exe) is a computer maintenance utility included in Microsoft Windows designed to free up disk space on a computer’s hard drive. The utility first searches and analyzes the hard drive for files that are no longer of any use, and then removes the unnecessary files. There are a number of different file categories that Disk Cleanup targets when performing the initial disk analysis:

  • Compression of old files
  • Temporary Internet files
  • Temporary Windows files
  • Downloaded program files
  • Recycle Bin
  • Removal of unused applications or optional Windows components
  • Setup log files
  • Offline web pages (cached)
  • WinSxS (Windows component store)

Disk Cleanup removes the above mentioned unwanted files from windows system drive securely without harming or affecting operating system functionalities. To Run this on your system, you can follow the below steps

  • Right Click on C or System Drive
  • Select Properties
  • Click on Disk CleanUp
  • On the Disk Clean Up utility Select the Files to Delete
  • Click Ok

If you want to do it using the PowerShell on a single machine or multiple machines with PowerShell Scripts, you can use the below script

$HKLM = [UInt32] “0x80000002”
$strKeyPath = “SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches”
$strValueName = “StateFlags0065”

$subkeys = gci -Path HKLM:\$strKeyPath -Name
ForEach ($subkey in $subkeys) {
Try {
New-ItemProperty -Path HKLM:\$strKeyPath\$subkey -Name $strValueName -PropertyType DWord -Value 2 -ErrorAction SilentlyContinue| Out-Null
}
Catch {
}
try {
Start-Process cleanmgr -ArgumentList “/sagerun:65” -Wait -NoNewWindow -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
catch {
}
}
ForEach ($subkey in $subkeys) {
Try {
Remove-ItemProperty -Path HKLM:\$strKeyPath\$subkey -Name $strValueName | Out-Null
}
Catch {
}
}

The above script will run on the machine with no prompts are given. meaning that it doesn’t need human action to run. you can schedule this script using Task Scheduler in Windows systems and it will run on the scheduled time.

If you want to log an Event in the Event logs, you can refer the below article and insert the codes to generate logs.

[How To] Create EventLog using PowerShell 

[How To]Use Event Logs in PowerShell Scripts

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar