Powershell: Clean up Hard Drive (C:\)

Hey You

It’s a very warm and sunny Friday and I hope your week was just as productive as mine was. 

Not so long ago a colleague and friend of mine, Jeroen Burgerhout, released in his blog an article he wrote about how to Remove HP Bloatware on Windows 10. It’s a good read and I do recommend you read it.

The article he wrote reminded me about the Powershell script I wrote for a client which would clean up the C drive. I wanted to share this with you.

Full Hard Drive

Scenario

If you have read my previous Powershell article named Migrate user to new environment you would know that I worked at a client which handed out persistent VDI’s to their users with only 60GB. 

You can imagine that the C drive would fill up pretty easily not just because of the user but also because of the applications, windows updates, temporary files, recycle bin etc. So we are going to tackle this in 1 script by doing the following.

  1. Empty the Recycle bin
  2. Delete temp files and folders
  3. Delete previously downloaded update files
  4. Run disk cleaner
  5. Run DISM to clean up the image

So let’s start and throw out the trash.

Don’t forget to recycle though.

Disclaimer

I want to point out that I am no Powershell Master. I have not attended a Powershell course and I definitely do not claim that I am the best in Powershell. 

There are probably better ways to do this but here is the way that I did it. So if you are a Powershell Master… here’s a laugh (but don’t laugh to hard)

#First I want to define the recycle bin variable
#The "Namespace(0xA) is defined as the recycle bin. 
#I will add an article soon with all namespaces that you might need. 
$recycleBinShell = New-Object -ComObject Shell.Application
$recycleBinFolder = $recycleBinShell.Namespace(0xA)

#Second I want to define the %temp% (AppData\Local\Temp) folder. 
$tempFilesENV = Get-ChildItem "env:\TEMP"
$tempFiles = $tempFilesENV.Value

#Third I want to define the Windows Temp directory
$windowsTemp = "C:\Windows\Temp\*"

#Finally I want to define the Windows Software Distribution Folder
$winDist = "C:\Windows\SoftwareDistribution"

#Now the magic begins

#Remove items from the recycle bin
$recycleBinFolder.item() | %{Remove-Item $_.path -Recurse -Confirm:$false}

#Remove the temp files in AppData\Local\Temp
Remove-Item -Recure "$tempFiles\*"

#Remove old Windows Updates
#Note: By removing this you will lose the Updates History and it might 
#redownload everything at the next update if you already haven't installed it
Get-Service -Name WUAUSERV | Stop-Service
Remove-Item -Path $winDist -Recurse -Force
Get-Service -Name WUAUSERV | Start-Service

#Disk Clean up Tool
cleanmgr /sagerun:1 /VeryLowDisk /AUTOCLEAN | Out-Null

#DISM
#First... let's repair what's broken
dism.exe /Online /Cleanup-Image /RestoreHealth

#Analyze (probably not needed)
dism.exe /Online /Cleanup-Image /AnalyzeComponentStore

#Delete junk
dism.exe /Online /Cleanup-Image /StartComponentCleanup

#Delete superseded junk
dism.exe /Online /Cleanup-Image /SPSuperseded

Overkill?

I know that the DISM functions at the end might be a tad overkill…

Too much overkill???
Too much overkill???

… but it’s not going to hurt. Worst case scenario; you need to wait a little longer.

Task Scheduler

It was suggested by a colleague that we could create a task in task scheduler inside the VDI that executes the script once a day but THAT would be overkill.

However; running it once a month or once a quarter might not be a bad idea.

Thanks for reading.

Cheers,

Engin