Windows cleaning script (PS)

An example of the windows cleaning script.

clean_windows_v1_1.ps1

To allow run scripts, use it if needed.

Set-ExecutionPolicy RemoteSigned

To run the script in PowerShell:

.\clean_windows_v1_1.ps1
# Powershell
# Script ver 1.1
# Stop all update services
Stop-Service -Name wuauserv -Force
Stop-Service -Name bits -Force
Stop-Service -Name cryptsvc -Force

# Cleaning Windows update cache
Write-Host "Clean Windows update cache ..."
Remove-Item -Path "C:\Windows\SoftwareDistribution\Download\*" -Recurse -Force
Remove-Item -Path "C:\Windows\SoftwareDistribution\DataStore\*" -Recurse -Force

# Start update services
Start-Service -Name wuauserv
Start-Service -Name bits
Start-Service -Name cryptsvc
Write-Host "Update services have been started."

# Cleaning the system`s temp files
Write-Host "Cleaning the system`s temp files ..."
Remove-Item -Path "C:\Windows\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue

# Cleaning all users temp files
Write-Host "Cleaning all users temp files ..."
$users = Get-ChildItem 'C:\Users' | Where-Object { $_.PSIsContainer }
foreach ($user in $users) {
    $tempPath = "$($user.FullName)\AppData\Local\Temp\"
    if (Test-Path $tempPath) {
        Remove-Item -Path "$tempPath\*" -Recurse -Force -ErrorAction SilentlyContinue
    }
}

# Deleting PDF files older than 60 days in the D:\doc\pdf
Write-Host "Deleting PDF files older than 60 days in the D:\doc\pdf ..."
$pathToPDFs = "D:\doc\pdf\"
$oldFiles = Get-ChildItem -Path $pathToPDFs -Filter *.pdf | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-60) }
foreach ($file in $oldFiles) {
    Remove-Item -Path $file.FullName -Force
}

Write-Host "Cleaning complete."